数组
1、固定数组
1.创建数组(不可变数组创建后不可变,在创建的时候要初始化)//对象方法 [[NSArray alloc] initWithObjects:(id),nil]NSArray* array = [[NSArrayalloc] initWithObjects:@"one",@"2",@"2.3",@"a",nil];//array == (one,2,"2.3",a);//类方法 [NSArray arrayWithObjects:(id),nil]NSArray* array1 = [NSArrayarrayWithObjects:@"one",@"2",@"2.3",@"a",nil];//array == (one,2,"2.3",a);//次方法最后一个元素必须是nil,如果数组中间存在 nil 元素,容易导致数据丢失NSString* str =nil;NSArray* array4 = [[NSArrayalloc] initWithObjects:@"oe",@"2",str,@"t3",@"3.14",nil];//array4 = (oe,2);//快速方法 @[(id)]NSArray* array3 = @[@"one#",@"23",@"2.3",@"a"];//array = ("one#",23,"2.32.基本类型转换成数组对象//数组中可以存储不同类型的对象inti =10;floatf =3.14;NSNumber* number = [NSNumbernumberWithInt:i];NSNumber* number1 = [NSNumbernumberWithFloat:f];NSArray* array = @[@"one",@"10",number,number1];// array == (one,10,10,"3.14");//数组中存储的是对象的地址,数组中也可以存储数组的地址NSArray* array1 = @[@"1",@"2",@"3"];NSArray* array2 = @[array,array1];3.数组中自定义对象//创建三个对象(类以及创建)Person * p1 = [[Person alloc] initWithName:@"Jack"andAge:15]; Person * p2 = [[Person alloc] initWithName:@"Tom"andAge:20]; Person * p3 = [[Person alloc] initWithName:@"Lucy"andAge:18];NSArrayarray4 = @[p1,p2,p3];4.获取数组中的元素 Person * p = [array4 objectAtIndex:0];//name = Jack,age = 15Person * p = array4[0];//快速方法5.数组中元素个数NSUIntegercount = [array4 count];NSLog(@"%lu",count);6.判断数组中是否包含某个元素 Person * p4 = [[Person alloc] initWithName:@"Tom"andAge:20];NSLog(@"p2=%p,p4=%p",p2,p4);//p2和p4的地址不相同,p4为新对象if([array4 containsObject:p4]) {NSLog(@"包含"); }else{NSLog(@"不包含"); }//不包含,p4不在array4中,即使内容相同,但是也不包含NSArray* array5 = @[@"one",@"two",@"three"];NSString* str2 =@"one";NSString* str4 =@"one";NSString* str3 = [[NSStringalloc] initWithFormat:@"%@",@"two"];NSLog(@"%p %p %p %p",str2,str3,str4,array9[0]);//0x100002088 0x6f777435 0x100002088//在创建字符串的时候会判断内存是否含有相同字符串,若有相同的则不会新开辟内存去存储,只把新的字符串指针指向那个地址,若没有相同的则就会新开辟空间去存储//但是 str3 是重新初始化 NSString,所以str3的地址是固定的 并不和array[1]的地址一样if([array9 containsObject:str3]) {NSLog(@"包含"); }else{NSLog(@"不包含"); }//包含7.遍历数组NSArray* array = @[@"1",@"2",@"three",@"4"];//方法一for(inti=0; i<[array count]; i++) {NSLog(@"array[%d]=%@",i,array[i]); }//方法二for(id* strinarray) {NSLog(@"%@",str); }8.数组排序(排序后放在一个新的NSArray数组中)NSArray* array = @[@"a",@"b",@"f",@"d",@"c"];//传入一个比较大小的方法 根据返回值来决定是否需要交换元素NSArray* array2 = [array sortedArrayUsingSelect:sel]; SEL sel =@selector(compare:);//a-b-c-d-fSEL sel2 =@selector(isGreatThan:);//a-b-c-d-fSEL sel3 =@selector(isLessThan:);//f-d-c-b-a//block//返回值 (^名字)(参数列表)NSArray* array3 = [array sortedArrayUsingComparator:^NSComparisonResult(idobj1,idobj2) {return[obj1 compare:obj2]; }];//a-b-c-d-fNSArray* array4 = [array sortedArrayUsingComparator:^NSComparisonResult(idobj1,idobj2) {return[obj1 compare:obj2]; }];//f-d-c-b-a
2、可变数组
1.创建可变数组NSArray* array = @[@"one",@"two",@"three",@"four"];NSMutableArray* muArr = [[NSMutableArrayalloc] initWithArray:array];NSMutableArray* muArr2 = [NSMutableArrayarrayWithObjects:@"one",@"two",@"three",nil];2.添加元素NSMutableArray* muArr3 = [[NSMutableArrayalloc] init]; [muArr3 addObject:@"one"];3.添加其他数组的元素 [muArr3 addObjectsFromArray:array];4.在指定位置插入元素 [muArr3 insetObject:@"a"atIndex:1];5.删除元素 会通过对象地址删除数组中所有的用同一个地址的对象 [muArr removeObject:@"one"]; //删除数组中的所有指向"one"地址的元素6.通过索引方式删除对象(索引值不能数组越界) [muArr removeObjectAtIndex:0];7.删除所有元素 [muArr removeAllObjects];8.交换数组元素 //- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2; [muArr exchangeObjectAtIndex:i withObjectAtIndex:j]
3、数组转换
1.不可变数组到可变数组 NSArray * array = @[@"one",@"two"]; NSMutableArray * muArr = [[NSMutableArray alloc] initWithArray:array];// [array mutableCopy];2.可变数组变成不可变数组 [muArr copy]3.NSString转换为NSArray//按照给定的字符串进行截取,将截取的多段字符串放入数组中- (NSArray *)componentsSeparatedByString:(NSString *)separator;// 例:有一个字符串,通过.进行分割NSString *string = @“www.lanou3g.com";NSArray *array = [string componentsSeparatedByString:@"."];NSLog(@"%@", array);输出结果:( www, lanou3g, com)4.NSArray转换为NSString//将数组中的元素按照给定的字符串格式拼接成一个完整的字符串对象- (NSString *)componentsJoinedByString:(NSString *)separator;//例:有一个数组,通过&将所有元素拼接成一个字符串NSArray *array = @[@"北京", @"大连", @"河南", @"上海", @"广州", @"西安"];NSString *string = [array componentsJoinedByString:@"&"];NSLog(@"%@", string);输出结果:北京&大连&河南&上海&广州&西安
字典
1、不可变字典
1.创建不可变字典NSDictionary* dic = [[NSDictionaryalloc] initWithObjectsAndKeys:@"one",@"1",@"two",@"2",nil];//快速创建字典NSDictionary* dic1 = @{@"3":@"three",@"4":@"four"};2.字典可以存储任意类型的对象NSArray* array = @[@"one",@"333"];NSNumber* num = [NSNumbernumberWithInt:10];NSDicitonary* dic2 = @{@"dic":dic,@"num":num,@"array":array};3.获取字典的长度(键的个数)NSUIntegercount = [dic2 count];4.从字典中取值NSString* arr = [dic3 objectForKey:@"array"];//快速取值NSDictionary* dic4 = dic3[@"dic"];NSNumber* number = dic3[@"num"];
2、可变字典
1.创建可变字典NSMutableDictionary* muDic = [[NAMutableDictionary alloc] initWithObjectsAndKeys:@"one",@"1",nil];//向可变字典中添加不可变字典NSDictionary* dic = @{@"3":@"three"};NSMutableDictionary* muDic2 = [[NSMutableDictionaryalloc] initWithDictionary:dic];2.向字典中插入数据 [muDic2 setObject:@"two"forKey:@"2"];3.遍历字典NSArray* allKeys = [muDic2 allKeys];for(idkeyinallKeys) {NSLog(@"%@",key);idobj = muDic2[key];NSLog(@"%@",obj); }4.删除数据 [muDic2 removeObjectForKey:@"2"];5.全部删除 [muDic removeAllObjects];
集合
//NSSet 是无序的,不能存储重复数据,可以用来去除重复数据
//NSArray 是自然顺序
1、不可变集合
1.创建 NSSet *set= [[NSSetalloc] initWithObjects:@"one",@"b",@"two",@"three",@"a"@"two",nil];2.个数 NSUInteger count = [setcount];3.判断是否包含某个对象 BOOL isContation = [setcontationObject:@"t"];if(isContation) { NSLog(@"contation"); }else{ NSLog(@"not contation"); }
2、可变集合
1.创建NSMutableSet* muSet = [[NSMutableSetalloc] initWithObjects:@"1",@"2",@"3",nil];2.添加对象 [muSet addObject:@"four"];3.删除对象 [muSet removeObject:@"2"];4.删除所有对象 [muSet removeAllObjects];
相互转化
1.数组-->集合 NSSet * set = [[NSSet alloc] initWithArray:array];2.字典-->数组 NSDictionary *dic= @{@"1":@"two",@"2":@"kk"};NSArray * keysArr = [dicallKeys];NSArray * valuaesArr = [dicallValues];3.字符串-->数组 NSString * str = @"I am in shanghai";NSArray * strArr = [str componentsSeparatedByString:@" "];4.数组-->字符串 NSString * str1 = [strArr componentsJoinedByString:@"-"];