數(shù)組
1董朝、固定數(shù)組
1.創(chuàng)建數(shù)組(不可變數(shù)組創(chuàng)建后不可變抓于,在創(chuàng)建的時(shí)候要初始化)//對(duì)象方法 [[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);//次方法最后一個(gè)元素必須是nil恤左,如果數(shù)組中間存在 nil 元素昂羡,容易導(dǎo)致數(shù)據(jù)丟失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.基本類型轉(zhuǎn)換成數(shù)組對(duì)象//數(shù)組中可以存儲(chǔ)不同類型的對(duì)象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");//數(shù)組中存儲(chǔ)的是對(duì)象的地址阀参,數(shù)組中也可以存儲(chǔ)數(shù)組的地址NSArray* array1 = @[@"1",@"2",@"3"];NSArray* array2 = @[array,array1];3.數(shù)組中自定義對(duì)象//創(chuàng)建三個(gè)對(duì)象(類以及創(chuàng)建)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.獲取數(shù)組中的元素? ? ? Person * p = [array4 objectAtIndex:0];//name = Jack,age = 15Person * p = array4[0];//快速方法5.數(shù)組中元素個(gè)數(shù)NSUIntegercount = [array4 count];NSLog(@"%lu",count);6.判斷數(shù)組中是否包含某個(gè)元素? ? ? Person * p4 = [[Person alloc] initWithName:@"Tom"andAge:20];NSLog(@"p2=%p,p4=%p",p2,p4);//p2和p4的地址不相同乳幸,p4為新對(duì)象if([array4 containsObject:p4]) {NSLog(@"包含");? ? ? }else{NSLog(@"不包含");? ? ? }//不包含瞪讼,p4不在array4中,即使內(nèi)容相同粹断,但是也不包含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//在創(chuàng)建字符串的時(shí)候會(huì)判斷內(nèi)存是否含有相同字符串符欠,若有相同的則不會(huì)新開辟內(nèi)存去存儲(chǔ),只把新的字符串指針指向那個(gè)地址瓶埋,若沒有相同的則就會(huì)新開辟空間去存儲(chǔ)//但是 str3 是重新初始化 NSString背亥,所以str3的地址是固定的 并不和array[1]的地址一樣if([array9 containsObject:str3]) {NSLog(@"包含");? ? ? }else{NSLog(@"不包含");? ? ? }//包含7.遍歷數(shù)組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.數(shù)組排序(排序后放在一個(gè)新的NSArray數(shù)組中)NSArray* array = @[@"a",@"b",@"f",@"d",@"c"];//傳入一個(gè)比較大小的方法 根據(jù)返回值來決定是否需要交換元素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//返回值 (^名字)(參數(shù)列表)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、可變數(shù)組
1.創(chuàng)建可變數(shù)組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.添加其他數(shù)組的元素? ? ? [muArr3 addObjectsFromArray:array];4.在指定位置插入元素? ? ? [muArr3 insetObject:@"a"atIndex:1];5.刪除元素? 會(huì)通過對(duì)象地址刪除數(shù)組中所有的用同一個(gè)地址的對(duì)象? ? ? [muArr removeObject:@"one"];? ? //刪除數(shù)組中的所有指向"one"地址的元素6.通過索引方式刪除對(duì)象(索引值不能數(shù)組越界)? ? ? [muArr removeObjectAtIndex:0];7.刪除所有元素? ? ? [muArr removeAllObjects];8.交換數(shù)組元素? ? //- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;? ? ? [muArr exchangeObjectAtIndex:i withObjectAtIndex:j]
3悬赏、數(shù)組轉(zhuǎn)換
1.不可變數(shù)組到可變數(shù)組? ? ? NSArray * array = @[@"one",@"two"];? ? ? NSMutableArray * muArr = [[NSMutableArray alloc] initWithArray:array];// [array mutableCopy];2.可變數(shù)組變成不可變數(shù)組? ? ? [muArr copy]3.NSString轉(zhuǎn)換為NSArray//按照給定的字符串進(jìn)行截取狡汉,將截取的多段字符串放入數(shù)組中- (NSArray *)componentsSeparatedByString:(NSString *)separator;// 例:有一個(gè)字符串,通過.進(jìn)行分割NSString *string = @“www.lanou3g.com";NSArray *array = [string? ? componentsSeparatedByString:@"."];NSLog(@"%@", array);輸出結(jié)果:(? www,? lanou3g,? com)4.NSArray轉(zhuǎn)換為NSString//將數(shù)組中的元素按照給定的字符串格式拼接成一個(gè)完整的字符串對(duì)象- (NSString *)componentsJoinedByString:(NSString *)separator;//例:有一個(gè)數(shù)組闽颇,通過&將所有元素拼接成一個(gè)字符串NSArray *array = @[@"北京", @"大連", @"河南", @"上海", @"廣州", @"西安"];NSString *string = [array componentsJoinedByString:@"&"];NSLog(@"%@", string);輸出結(jié)果:北京&大連&河南&上海&廣州&西安
字典
1盾戴、不可變字典
1.創(chuàng)建不可變字典NSDictionary* dic = [[NSDictionaryalloc] initWithObjectsAndKeys:@"one",@"1",@"two",@"2",nil];//快速創(chuàng)建字典NSDictionary* dic1 = @{@"3":@"three",@"4":@"four"};2.字典可以存儲(chǔ)任意類型的對(duì)象NSArray* array = @[@"one",@"333"];NSNumber* num = [NSNumbernumberWithInt:10];NSDicitonary* dic2 = @{@"dic":dic,@"num":num,@"array":array};3.獲取字典的長(zhǎng)度(鍵的個(gè)數(shù))NSUIntegercount = [dic2 count];4.從字典中取值NSString* arr = [dic3 objectForKey:@"array"];//快速取值NSDictionary* dic4 = dic3[@"dic"];NSNumber* number = dic3[@"num"];
2、可變字典
1.創(chuàng)建可變字典NSMutableDictionary* muDic = [[NAMutableDictionary alloc] initWithObjectsAndKeys:@"one",@"1",nil];//向可變字典中添加不可變字典NSDictionary* dic = @{@"3":@"three"};NSMutableDictionary* muDic2 = [[NSMutableDictionaryalloc] initWithDictionary:dic];2.向字典中插入數(shù)據(jù)? [muDic2 setObject:@"two"forKey:@"2"];3.遍歷字典NSArray* allKeys = [muDic2 allKeys];for(idkeyinallKeys) {NSLog(@"%@",key);idobj = muDic2[key];NSLog(@"%@",obj);? }4.刪除數(shù)據(jù)? [muDic2 removeObjectForKey:@"2"];5.全部刪除? [muDic removeAllObjects];
集合
//NSSet 是無序的兵多,不能存儲(chǔ)重復(fù)數(shù)據(jù)尖啡,可以用來去除重復(fù)數(shù)據(jù)
//NSArray 是自然順序
1、不可變集合
1.創(chuàng)建? ? ? NSSet *set= [[NSSetalloc] initWithObjects:@"one",@"b",@"two",@"three",@"a"@"two",nil];2.個(gè)數(shù)? ? ? NSUInteger count = [setcount];3.判斷是否包含某個(gè)對(duì)象? ? ? BOOL isContation = [setcontationObject:@"t"];if(isContation) {? ? ? ? ? NSLog(@"contation");? ? ? }else{? ? ? ? ? NSLog(@"not contation");? ? ? }
2剩膘、可變集合
1.創(chuàng)建NSMutableSet* muSet = [[NSMutableSetalloc] initWithObjects:@"1",@"2",@"3",nil];2.添加對(duì)象? ? ? [muSet addObject:@"four"];3.刪除對(duì)象? ? ? [muSet removeObject:@"2"];4.刪除所有對(duì)象? ? ? [muSet removeAllObjects];
相互轉(zhuǎn)化
1.數(shù)組-->集合? NSSet * set = [[NSSet alloc] initWithArray:array];2.字典-->數(shù)組? NSDictionary *dic= @{@"1":@"two",@"2":@"kk"};NSArray * keysArr = [dicallKeys];NSArray * valuaesArr = [dicallValues];3.字符串-->數(shù)組? NSString * str = @"I am in shanghai";NSArray * strArr = [str componentsSeparatedByString:@" "];4.數(shù)組-->字符串? NSString * str1 = [strArr componentsJoinedByString:@"-"];