Objective-C 集合

import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

// 数组
// 不可变数组
// 创建方式
// 初始化时填写需要放到数组里的对象, 以nil结尾作为数组的结尾
NSArray *array = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", nil];

// 便利构造器的创建方式
NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];    

// 字面量的创建方式
// 字面量创建的是不可变的数组, 不需要在使用nil作为结束(系统会自动添加), 创建时不能放入为nil的对象, 会导致程序崩溃
NSArray *arrya2 = @[@"a", @"b", @"c", @"d"];     

// 数组元素访问
// 数组元素个数
NSLog(@"count: %lu", array.count);    

// 通过索引值访问数组元素个数
// 越界访问会引起程序崩溃
NSLog(@"object: %@", [array objectAtIndex:2]);

// 通过对象获取在数组中的对应的索引值(当数组中含有多个此对象时, 获取到第一个出现的索引值时结束, 当数组中不含有这个元素时, 获取的索引值为一个特别大的值)
NSLog(@"index: %lu", [array indexOfObject:@"b"]);

// 通过字面量的形式访问数组
NSLog(@"object: %@", array[0]);

// 拓展字符串方法
// 可以将一个字符串以分隔标志以分割标志进行分割, 如下面案例以@"." 将网站分割开, 将分割后的子字符串以数组的形式返回
NSString *urlString = @"www.lanou3g.com";
NSArray *resultArray = [urlString componentsSeparatedByString:@"."];
NSLog(@"%@", urlString);
NSLog(@"%@", resultArray);
NSLog(@"lanou result: %@", resultArray[1]);    

// 将数组中的字符串对象拼接合成一个字符串
// @"www&lanou3g&com"
NSString *resultString = [resultArray componentsJoinedByString:@"&"];
NSLog(@"result String: %@", resultString);

// 数组中是否包含一个对象
if ([array containsObject:@"a"]) {
    NSLog(@"包含");
} else {
    NSLog(@"不包含");
}
 
/*
 存在如下的字符串,将其中的图片的网址提取出来。
 “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]);
    
/*
 有如下一个数组,@[@“type = iOS", @"Device = iPhone", @"count = 11344115@163.com", @"password = 12345”],将其中的内容用”&"符号拼接成一个字符串
 */    
NSArray *joiningString = @[@"type = iOS", @"Device = iPhone", @"count = 11344115@163.com", @"password = 12345"];    
NSString *joining = [joiningString componentsJoinedByString:@" & "];    
NSLog(@"String concatenation: %@", joining);

// 空数组是一个有效的数组对象, 只不过数组元素中没有任何元素
// nilArray 指的是当前对象的指针指向一个空地址, 不是有效对象
NSArray *emptyArray = [[NSArray alloc] init];
NSArray *nilArray = nil;

// 获取数组的第一个元素
NSLog(@"frist: %@", [array firstObject]);
NSLog(@"last: %@", [array lastObject]);

// 可变数组使用方式与不可变完全相同, 只不过多了对数组元素的增加, 修改和删除操作
// 创建方式
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];

// 使用便利构造器创建一个空的可变数组
NSMutableArray *mutableArray1 = [NSMutableArray array];

// 不可变数组的创建方式(除了字面量) 可变数组都有
NSMutableArray *mutableArray2 = [[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", nil];   

// 通过一个不可变数组去初始化一个可变数组
NSMutableArray *mutableArray3 = [NSMutableArray arrayWithArray:array];
NSLog(@"%@", mutableArray3);   

// 增加一个元素对象
[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);

// 插入一个元素对象
// 将一个对象插入到某一个位置(索引值)
[mutableArray insertObject:@"w" atIndex:0];
NSLog(@"inser array: %@", mutableArray);

// 修改某个对象
[mutableArray replaceObjectAtIndex:1 withObject:@"d"];
NSLog(@"replace array: %@", mutableArray);

// 以字面量的形式修改某个对象
mutableArray[2] = @"123";
NSLog(@"result array: %@", mutableArray);

// 交换
[mutableArray exchangeObjectAtIndex:0 withObjectAtIndex:2];
NSLog(@"exchange array: %@", mutableArray);   

// 删除某个对象
// 当数组中含有多个要移除的对象时, 调用这个方法会全部移除
// 当数组中不含有这个对象时, 移出操作没有任何效果
[mutableArray removeObject:@"123"];
NSLog(@"remove array: %@", mutableArray);

// 通过索引值移除数组中某个元素
[mutableArray removeObjectAtIndex:0];
NSLog(@"remove index array: %@", mutableArray);

// 移除数组中第一次出现的某一个对象
NSUInteger index = [mutableArray indexOfObject:@"w"];
[mutableArray removeObjectAtIndex:index];
NSLog(@"remove frist appear: %@", mutableArray); 

// 可以这么写, 先判断数组中是否有那个元素
if ([mutableArray containsObject:@"123"]) {        
     NSUInteger index1 = [mutableArray indexOfObject:@"123"];       
     [mutableArray removeObjectAtIndex:index1];
     NSLog(@"remove frist appear: %@", mutableArray);
}

// 移除某一范围的特定对象
[mutableArray removeObject:@"w" inRange:NSMakeRange(0, 3)];
NSLog(@"remove in range after: %@", mutableArray);

// 移除某一范围的所有对象
[mutableArray removeObjectsInRange:NSMakeRange(0, 2)];
NSLog(@"remove object in range: %@", mutableArray);

// 移除与某个数组的交集元素
[mutableArray removeObjectsInArray:@[@"w", @"2"]];
NSLog(@"remove in array: %@", mutableArray);

// 移除最后一个对象
[mutableArray removeLastObject];
NSLog(@"remove last object: %@", mutableArray);

// 移除所有对象
[mutableArray removeAllObjects];
NSLog(@"remove all object: %@", mutableArray);

// 把一个数组中的所有对象添加到当前数组
[mutableArray addObjectsFromArray:array];
NSLog(@"add object: %@", mutableArray); 

// 字典
// 键值对key-value key和value要求必须是对象类型的
// key必须是唯一的, 只能对应一个value, 同一个value可以对应多个key
// 无序的    
// 不可变字典
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"99", @"age", @"small tiger", @"name", @"???", @"sxe",  nil];

// 便利构造器
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"99", @"age", @"small tiger", @"name", @"???", @"sxe", nil];
NSLog(@"dic: %@", dic);

// 通过key访问一个value
NSLog(@"name: %@", [dic objectForKey:@"name"]);    

// 通过字面量访问一个value
NSLog(@"age: %@", dic[@"age"]);    

// 获取键值对个数
NSLog(@"count: %lu", dic.count);

// 获取所有的value
NSLog(@"all value: %@", dic.allValues);

// 获取所有的key
NSLog(@"all key: %@", dic.allKeys);



// 字面量的创造方式
NSDictionary *dic2 = @{@"name": @"small tiger", @"age": @"99", @"sex": @"???"};
NSLog(@"dic2: %@", dic2);    

// 通过value数组和key数组初始化一个字典
NSDictionary *dic3 = [NSDictionary dictionaryWithObjects:@[@"small tiger", @"99", @"???"] forKeys:@[@"name", @"age", @"sex"]];
NSLog(@"dic3: %@", dic3);    

// 可变字典
// 创建一个空字典
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionary];    

// 添加(当前的key不存在)
[mutableDic setObject:@"add more baby" forKey:@"name"];
[mutableDic setValue:@"10" forKey:@"age"];  // setObject 和setValue 是一样的效果, setObject 是字典里的方法, setValue是所有对象里都有的, 为了使对象和字典产生关联
[mutableDic setObject:@"???" forKey:@"sex"];
NSLog(@"mutableDic: %@", mutableDic);    

// 修改(同样的方法, 当前的key已经存在)
[mutableDic setObject:@"king old luck" forKey:@"name"];
NSLog(@"mutableDic: %@", mutableDic);    

// 字面量的修改方式
mutableDic[@"name"] = @"old driver";
NSLog(@"change: %@", mutableDic);    

// // 删除某个键值对
// [mutableDic removeObjectForKey:@”name”];
// NSLog(@”remove: %@”, mutableDic);

// // 删除某个键值对
// [mutableDic removeAllObjects];
// NSLog(@”remove all: %@”, mutableDic);

// 删除key数组对应的键值对
[mutableDic removeObjectsForKeys:@[@"age", @"sex"]];
NSLog(@"remove object: %@", mutableDic);    

// 通过value查找所有对应的的key
NSDictionary *testDic = @{@"name": @"small", @"age": @"small", @"sex": @"small"};
NSArray *allKeys = [testDic allKeysForObject:@"small"];
NSLog(@"all key: %@", allKeys);        

// 集合
// 无序的, 不能放入重复的元素    
// 不可变集合
NSSet *set = [[NSSet alloc] initWithObjects:@"a", @"b", @"a", @"b", nil];
NSLog(@"set: %@", set);    
NSLog(@"count: %lu", set.count);    

// 获取集合中的某一个对象, 不能保证随机性
[set anyObject];
NSLog(@"%@", [set anyObject]);

// 获取所有元素
NSArray *setAllObject = [set allObjects];
NSLog(@"all object: %@", setAllObject);

// 判断是否含有一个对象
[set containsObject:@"a"];
NSLog(@"%d", [set containsObject:@"a"]);

// 可变集合
// 放入重复数据, 不显示
NSMutableSet *mutableSet = [NSMutableSet setWithArray:@[@"a", @"b", @"a", @"b"]];
NSLog(@"mutable set: %@", mutableSet);

// 添加一个元素(原来有的不添加, 没有的才添加)
[mutableSet addObject:@"w"];
NSLog(@"add set: %@", mutableSet);    

// 添加几个元素(原来有的不添加, 没有的才添加)
[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;

}

    原文作者:大闸蟹的江山
    原文地址: https://www.jianshu.com/p/525f86fd7f33
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞