import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
// 數(shù)組
// 不可變數(shù)組
// 創(chuàng)建方式
// 初始化時(shí)填寫(xiě)需要放到數(shù)組里的對(duì)象, 以nil結(jié)尾作為數(shù)組的結(jié)尾
NSArray *array = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", nil];
// 便利構(gòu)造器的創(chuàng)建方式
NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
// 字面量的創(chuàng)建方式
// 字面量創(chuàng)建的是不可變的數(shù)組, 不需要在使用nil作為結(jié)束(系統(tǒng)會(huì)自動(dòng)添加), 創(chuàng)建時(shí)不能放入為nil的對(duì)象, 會(huì)導(dǎo)致程序崩潰
NSArray *arrya2 = @[@"a", @"b", @"c", @"d"];
// 數(shù)組元素訪問(wèn)
// 數(shù)組元素個(gè)數(shù)
NSLog(@"count: %lu", array.count);
// 通過(guò)索引值訪問(wèn)數(shù)組元素個(gè)數(shù)
// 越界訪問(wèn)會(huì)引起程序崩潰
NSLog(@"object: %@", [array objectAtIndex:2]);
// 通過(guò)對(duì)象獲取在數(shù)組中的對(duì)應(yīng)的索引值(當(dāng)數(shù)組中含有多個(gè)此對(duì)象時(shí), 獲取到第一個(gè)出現(xiàn)的索引值時(shí)結(jié)束, 當(dāng)數(shù)組中不含有這個(gè)元素時(shí), 獲取的索引值為一個(gè)特別大的值)
NSLog(@"index: %lu", [array indexOfObject:@"b"]);
// 通過(guò)字面量的形式訪問(wèn)數(shù)組
NSLog(@"object: %@", array[0]);
// 拓展字符串方法
// 可以將一個(gè)字符串以分隔標(biāo)志以分割標(biāo)志進(jìn)行分割, 如下面案例以@"." 將網(wǎng)站分割開(kāi), 將分割后的子字符串以數(shù)組的形式返回
NSString *urlString = @"www.lanou3g.com";
NSArray *resultArray = [urlString componentsSeparatedByString:@"."];
NSLog(@"%@", urlString);
NSLog(@"%@", resultArray);
NSLog(@"lanou result: %@", resultArray[1]);
// 將數(shù)組中的字符串對(duì)象拼接合成一個(gè)字符串
// @"www&lanou3g&com"
NSString *resultString = [resultArray componentsJoinedByString:@"&"];
NSLog(@"result String: %@", resultString);
// 數(shù)組中是否包含一個(gè)對(duì)象
if ([array containsObject:@"a"]) {
NSLog(@"包含");
} else {
NSLog(@"不包含");
}
/*
存在如下的字符串,將其中的圖片的網(wǎng)址提取出來(lái)林说。
“http://www.imanhua.com/Cover/201110/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg”
*/
NSString *extractString = @"http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg";
NSArray *extract = [extractString componentsSeparatedByString:@"&"];
NSLog(@"Extraction of string: %@", extract[0]);
/*
有如下一個(gè)數(shù)組,@[@“type = iOS", @"Device = iPhone", @"count = 11344115@163.com", @"password = 12345”]硼啤,將其中的內(nèi)容用”&"符號(hào)拼接成一個(gè)字符串
*/
NSArray *joiningString = @[@"type = iOS", @"Device = iPhone", @"count = 11344115@163.com", @"password = 12345"];
NSString *joining = [joiningString componentsJoinedByString:@" & "];
NSLog(@"String concatenation: %@", joining);
// 空數(shù)組是一個(gè)有效的數(shù)組對(duì)象, 只不過(guò)數(shù)組元素中沒(méi)有任何元素
// nilArray 指的是當(dāng)前對(duì)象的指針指向一個(gè)空地址, 不是有效對(duì)象
NSArray *emptyArray = [[NSArray alloc] init];
NSArray *nilArray = nil;
// 獲取數(shù)組的第一個(gè)元素
NSLog(@"frist: %@", [array firstObject]);
NSLog(@"last: %@", [array lastObject]);
// 可變數(shù)組使用方式與不可變完全相同, 只不過(guò)多了對(duì)數(shù)組元素的增加, 修改和刪除操作
// 創(chuàng)建方式
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
// 使用便利構(gòu)造器創(chuàng)建一個(gè)空的可變數(shù)組
NSMutableArray *mutableArray1 = [NSMutableArray array];
// 不可變數(shù)組的創(chuàng)建方式(除了字面量) 可變數(shù)組都有
NSMutableArray *mutableArray2 = [[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", nil];
// 通過(guò)一個(gè)不可變數(shù)組去初始化一個(gè)可變數(shù)組
NSMutableArray *mutableArray3 = [NSMutableArray arrayWithArray:array];
NSLog(@"%@", mutableArray3);
// 增加一個(gè)元素對(duì)象
[mutableArray addObject:@"1"];
[mutableArray addObject:@"2"];
[mutableArray addObject:@"w"];
[mutableArray addObject:@"d"];
[mutableArray addObject:@"y"];
[mutableArray addObject:@"w"];
[mutableArray addObject:@"w"];
[mutableArray addObject:@"456"];
[mutableArray addObject:@"w5m"];
[mutableArray addObject:@"2w"];
NSLog(@"add array: %@", mutableArray);
// 插入一個(gè)元素對(duì)象
// 將一個(gè)對(duì)象插入到某一個(gè)位置(索引值)
[mutableArray insertObject:@"w" atIndex:0];
NSLog(@"inser array: %@", mutableArray);
// 修改某個(gè)對(duì)象
[mutableArray replaceObjectAtIndex:1 withObject:@"d"];
NSLog(@"replace array: %@", mutableArray);
// 以字面量的形式修改某個(gè)對(duì)象
mutableArray[2] = @"123";
NSLog(@"result array: %@", mutableArray);
// 交換
[mutableArray exchangeObjectAtIndex:0 withObjectAtIndex:2];
NSLog(@"exchange array: %@", mutableArray);
// 刪除某個(gè)對(duì)象
// 當(dāng)數(shù)組中含有多個(gè)要移除的對(duì)象時(shí), 調(diào)用這個(gè)方法會(huì)全部移除
// 當(dāng)數(shù)組中不含有這個(gè)對(duì)象時(shí), 移出操作沒(méi)有任何效果
[mutableArray removeObject:@"123"];
NSLog(@"remove array: %@", mutableArray);
// 通過(guò)索引值移除數(shù)組中某個(gè)元素
[mutableArray removeObjectAtIndex:0];
NSLog(@"remove index array: %@", mutableArray);
// 移除數(shù)組中第一次出現(xiàn)的某一個(gè)對(duì)象
NSUInteger index = [mutableArray indexOfObject:@"w"];
[mutableArray removeObjectAtIndex:index];
NSLog(@"remove frist appear: %@", mutableArray);
// 可以這么寫(xiě), 先判斷數(shù)組中是否有那個(gè)元素
if ([mutableArray containsObject:@"123"]) {
NSUInteger index1 = [mutableArray indexOfObject:@"123"];
[mutableArray removeObjectAtIndex:index1];
NSLog(@"remove frist appear: %@", mutableArray);
}
// 移除某一范圍的特定對(duì)象
[mutableArray removeObject:@"w" inRange:NSMakeRange(0, 3)];
NSLog(@"remove in range after: %@", mutableArray);
// 移除某一范圍的所有對(duì)象
[mutableArray removeObjectsInRange:NSMakeRange(0, 2)];
NSLog(@"remove object in range: %@", mutableArray);
// 移除與某個(gè)數(shù)組的交集元素
[mutableArray removeObjectsInArray:@[@"w", @"2"]];
NSLog(@"remove in array: %@", mutableArray);
// 移除最后一個(gè)對(duì)象
[mutableArray removeLastObject];
NSLog(@"remove last object: %@", mutableArray);
// 移除所有對(duì)象
[mutableArray removeAllObjects];
NSLog(@"remove all object: %@", mutableArray);
// 把一個(gè)數(shù)組中的所有對(duì)象添加到當(dāng)前數(shù)組
[mutableArray addObjectsFromArray:array];
NSLog(@"add object: %@", mutableArray);
// 字典
// 鍵值對(duì)key-value key和value要求必須是對(duì)象類型的
// key必須是唯一的, 只能對(duì)應(yīng)一個(gè)value, 同一個(gè)value可以對(duì)應(yīng)多個(gè)key
// 無(wú)序的
// 不可變字典
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"99", @"age", @"small tiger", @"name", @"???", @"sxe", nil];
// 便利構(gòu)造器
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"99", @"age", @"small tiger", @"name", @"???", @"sxe", nil];
NSLog(@"dic: %@", dic);
// 通過(guò)key訪問(wèn)一個(gè)value
NSLog(@"name: %@", [dic objectForKey:@"name"]);
// 通過(guò)字面量訪問(wèn)一個(gè)value
NSLog(@"age: %@", dic[@"age"]);
// 獲取鍵值對(duì)個(gè)數(shù)
NSLog(@"count: %lu", dic.count);
// 獲取所有的value
NSLog(@"all value: %@", dic.allValues);
// 獲取所有的key
NSLog(@"all key: %@", dic.allKeys);
// 字面量的創(chuàng)造方式
NSDictionary *dic2 = @{@"name": @"small tiger", @"age": @"99", @"sex": @"???"};
NSLog(@"dic2: %@", dic2);
// 通過(guò)value數(shù)組和key數(shù)組初始化一個(gè)字典
NSDictionary *dic3 = [NSDictionary dictionaryWithObjects:@[@"small tiger", @"99", @"???"] forKeys:@[@"name", @"age", @"sex"]];
NSLog(@"dic3: %@", dic3);
// 可變字典
// 創(chuàng)建一個(gè)空字典
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionary];
// 添加(當(dāng)前的key不存在)
[mutableDic setObject:@"add more baby" forKey:@"name"];
[mutableDic setValue:@"10" forKey:@"age"]; // setObject 和setValue 是一樣的效果, setObject 是字典里的方法, setValue是所有對(duì)象里都有的, 為了使對(duì)象和字典產(chǎn)生關(guān)聯(lián)
[mutableDic setObject:@"???" forKey:@"sex"];
NSLog(@"mutableDic: %@", mutableDic);
// 修改(同樣的方法, 當(dāng)前的key已經(jīng)存在)
[mutableDic setObject:@"king old luck" forKey:@"name"];
NSLog(@"mutableDic: %@", mutableDic);
// 字面量的修改方式
mutableDic[@"name"] = @"old driver";
NSLog(@"change: %@", mutableDic);
// // 刪除某個(gè)鍵值對(duì)
// [mutableDic removeObjectForKey:@"name"];
// NSLog(@"remove: %@", mutableDic);
// // 刪除某個(gè)鍵值對(duì)
// [mutableDic removeAllObjects];
// NSLog(@"remove all: %@", mutableDic);
// 刪除key數(shù)組對(duì)應(yīng)的鍵值對(duì)
[mutableDic removeObjectsForKeys:@[@"age", @"sex"]];
NSLog(@"remove object: %@", mutableDic);
// 通過(guò)value查找所有對(duì)應(yīng)的的key
NSDictionary *testDic = @{@"name": @"small", @"age": @"small", @"sex": @"small"};
NSArray *allKeys = [testDic allKeysForObject:@"small"];
NSLog(@"all key: %@", allKeys);
// 集合
// 無(wú)序的, 不能放入重復(fù)的元素
// 不可變集合
NSSet *set = [[NSSet alloc] initWithObjects:@"a", @"b", @"a", @"b", nil];
NSLog(@"set: %@", set);
NSLog(@"count: %lu", set.count);
// 獲取集合中的某一個(gè)對(duì)象, 不能保證隨機(jī)性
[set anyObject];
NSLog(@"%@", [set anyObject]);
// 獲取所有元素
NSArray *setAllObject = [set allObjects];
NSLog(@"all object: %@", setAllObject);
// 判斷是否含有一個(gè)對(duì)象
[set containsObject:@"a"];
NSLog(@"%d", [set containsObject:@"a"]);
// 可變集合
// 放入重復(fù)數(shù)據(jù), 不顯示
NSMutableSet *mutableSet = [NSMutableSet setWithArray:@[@"a", @"b", @"a", @"b"]];
NSLog(@"mutable set: %@", mutableSet);
// 添加一個(gè)元素(原來(lái)有的不添加, 沒(méi)有的才添加)
[mutableSet addObject:@"w"];
NSLog(@"add set: %@", mutableSet);
// 添加幾個(gè)元素(原來(lái)有的不添加, 沒(méi)有的才添加)
[mutableSet addObjectsFromArray:@[@"d", @"y", @"a"]];
NSLog(@"add set: %@", mutableSet);
// 刪除
[mutableSet removeObject:@"w"];
NSLog(@"remove set: %@", mutableSet);
// 刪除所有元素
[mutableSet removeAllObjects];
NSLog(@"remove all set: %@", mutableSet);
return 0;
}