數(shù)組
- 1、固定數(shù)組
1.創(chuàng)建數(shù)組(不可變數(shù)組創(chuàng)建后不可變,在創(chuàng)建的時(shí)候要初始化)
//對(duì)象方法 [[NSArray alloc] initWithObjects:(id),nil]
NSArray * array = [[NSArray alloc] initWithObjects:@"one",@"2",@"2.3",@"a",nil]; //array == (one,2,"2.3",a);
//類方法 [NSArray arrayWithObjects:(id),nil]
NSArray * array1 = [NSArray arrayWithObjects:@"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 = [[NSArray alloc] initWithObjects:@"oe",@"2",str,@"t3",@"3.14", nil]; //array4 = (oe,2);
//快速方法 @[(id)]
NSArray * array3 = @[@"one#",@"23",@"2.3",@"a"]; //array = ("one#",23,"2.3
2.基本類型轉(zhuǎn)換成數(shù)組對(duì)象
//數(shù)組中可以存儲(chǔ)不同類型的對(duì)象
int i = 10;
float f = 3.14;
NSNumber * number = [NSNumber numberWithInt:i];
NSNumber * number1 = [NSNumber numberWithFloat: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];
NSArray array4 = @[p1,p2,p3];
4.獲取數(shù)組中的元素
Person * p = [array4 objectAtIndex:0]; //name = Jack,age = 15
Person * p = array4[0]; //快速方法
5.數(shù)組中元素個(gè)數(shù)
NSUInteger count = [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 = [[NSString alloc] initWithFormat:@"%@",@"two"];
NSLog(@"%p %p %p %p",str2,str3,str4,array9[0]); //0x100002088 0x6f777435 0x100002088
//在創(chuàng)建字符串的時(shí)候會(huì)判斷內(nèi)存是否含有相同字符串,若有相同的則不會(huì)新開(kāi)辟內(nèi)存去存儲(chǔ),只把新的字符串指針指向那個(gè)地址,若沒(méi)有相同的則就會(huì)新開(kāi)辟空間去存儲(chǔ)
//但是 str3 是重新初始化 NSString,所以str3的地址是固定的 并不和array[1]的地址一樣
if ([array9 containsObject:str3]) {
NSLog(@"包含");
} else {
NSLog(@"不包含");
}
//包含
7.遍歷數(shù)組
NSArray * array = @[@"1",@"2",@"three",@"4"];
//方法一
for (int i=0; i<[array count]; i++) {
NSLog(@"array[%d]=%@",i,array[i]);
}
//方法二
for (id * str in array) {
NSLog(@"%@",str);
}
8.數(shù)組排序(排序后放在一個(gè)新的NSArray數(shù)組中)
NSArray * array = @[@"a",@"b",@"f",@"d",@"c"];
//傳入一個(gè)比較大小的方法 根據(jù)返回值來(lái)決定是否需要交換元素
NSArray * array2 = [array sortedArrayUsingSelect:sel];
SEL sel = @selector(compare:); //a-b-c-d-f
SEL sel2 = @selector(isGreatThan:); //a-b-c-d-f
SEL sel3 = @selector(isLessThan:); //f-d-c-b-a
//block
//返回值 (^名字)(參數(shù)列表)
NSArray * array3 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}]; //a-b-c-d-f
NSArray * array4 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}]; //f-d-c-b-a
- 2、可變數(shù)組
1.創(chuàng)建可變數(shù)組
NSArray * array = @[@"one",@"two",@"three",@"four"];
NSMutableArray * muArr = [[NSMutableArray alloc] initWithArray:array];
NSMutableArray * muArr2 = [NSMutableArray arrayWithObjects:@"one",@"two",@"three",nil];
2.添加元素
NSMutableArray * muArr3 = [[NSMutableArray alloc] init];
[muArr3 addObject:@"one"];
3.添加其他數(shù)組的元素
[muArr3 addObjectsFromArray:array];
4.在指定位置插入元素
[muArr3 insetObject:@"a" atIndex:1];
5.刪除元素 會(huì)通過(guò)對(duì)象地址刪除數(shù)組中所有的用同一個(gè)地址的對(duì)象
[muArr removeObject:@"one"]; //刪除數(shù)組中的所有指向"one"地址的元素
6.通過(guò)索引方式刪除對(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è)字符串,通過(guò).進(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ù)組,通過(guò)&將所有元素拼接成一個(gè)字符串
NSArray *array = @[@"北京", @"大連", @"河南", @"上海", @"廣州", @"西安"];
NSString *string = [array componentsJoinedByString:@"&"];
NSLog(@"%@", string);
輸出結(jié)果:北京&大連&河南&上海&廣州&西安
字典
- 1、不可變字典
1.創(chuàng)建不可變字典
NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"one",@"1",@"two",@"2",nil];
//快速創(chuàng)建字典
NSDictionary * dic1 = @{@"3":@"three",@"4":@"four"};
2.字典可以存儲(chǔ)任意類型的對(duì)象
NSArray * array = @[@"one",@"333"];
NSNumber * num = [NSNumber numberWithInt:10];
NSDicitonary * dic2 = @{@"dic":dic,@"num":num,@"array":array};
3.獲取字典的長(zhǎng)度(鍵的個(gè)數(shù))
NSUInteger count = [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 = [[NSMutableDictionary alloc] initWithDictionary:dic];
2.向字典中插入數(shù)據(jù)
[muDic2 setObject:@"two" forKey:@"2"];
3.遍歷字典
NSArray * allKeys = [muDic2 allKeys];
for (id key in allKeys) {
NSLog(@"%@",key);
id obj = muDic2[key];
NSLog(@"%@",obj);
}
4.刪除數(shù)據(jù)
[muDic2 removeObjectForKey:@"2"];
5.全部刪除
[muDic removeAllObjects];
集合
//NSSet 是無(wú)序的,不能存儲(chǔ)重復(fù)數(shù)據(jù),可以用來(lái)去除重復(fù)數(shù)據(jù)
//NSArray 是自然順序
- 1、不可變集合
1.創(chuàng)建
NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"b",@"two",@"three",@"a"@"two",nil];
2.個(gè)數(shù)
NSUInteger count = [set count];
3.判斷是否包含某個(gè)對(duì)象
BOOL isContation = [set contationObject:@"t"];
if (isContation) {
NSLog(@"contation");
} else {
NSLog(@"not contation");
}
- 2、可變集合
1.創(chuàng)建
NSMutableSet * muSet = [[NSMutableSet alloc] 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 = [dic allKeys];
NSArray * valuaesArr = [dic allValues];
3.字符串-->數(shù)組
NSString * str = @"I am in shanghai";
NSArray * strArr = [str componentsSeparatedByString:@" "];
4.數(shù)組-->字符串
NSString * str1 = [strArr componentsJoinedByString:@"-"];