C25_OC15-OC集合类
2014-05-11 16:58
417 查看
1.NSArray数组
// // main.m // foundation框架 // // Created by vigiles on 14-5-10. // Copyright (c) 2014年 beardap. All rights reserved. // #import <Foundation/Foundation.h> void test() { /** 这两个永远是空数组 **/ NSArray * arr1 = [NSArray new]; NSArray * arr2 = [NSArray array]; /** OC数组只能存放OC对象,且不能存放“nil” **/ /** OC数组只能在创建时初始化 **/ // 只有一个元素 NSArray * arr3 = [NSArray arrayWithObject:@"只有一个元素"]; // 多个元素 NSArray * arr4 = [NSArray arrayWithObjects:@"第一个元素", @"第二个元素", @"第三个元素", nil]; NSArray * arr5 = @[@"第1个元素", @"第2个元素", @"第3个元素"]; /** 使用 **/ // 点方法获取数组长度-元素个数 arr4.count; // 消息指令获取元素个数 [arr4 count]; // 类方法根据索引获取元素 [arr4 objectAtIndex:2]; // C模式根据索引获取元素 arr4[0]; // 遍历元素 for (id obj in arr5) { // 获取元素的索引 [arr5 indexOfObject:obj]; NSLog(@"%@", obj); } // 使用Block遍历元素 [arr5 enumerateObjectsUsingBlock: // 每枚举一条记录,即更新id、idx、*stop ^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"%ld - %@", idx, obj); // 终止遍历 if (idx == 1) *stop = YES; // 注意:是*stop } ]; } void codeLineCount(NSString *path){ // 1. 文件资源管理器 // 整个程序运行中只有一个NSFM对象-单例 NSFileManager *nsfm = [NSFileManager defaultManager]; BOOL dir = NO; // 是否为目录 // 2. 判断路径是否存在,是否为目录 BOOL exist = [nsfm fileExistsAtPath:path isDirectory:&dir]; // 如果是文件夹,赋值dir为YES // 3. 不存在就退出 if (!exist) return; // 4. 如果是目录 if (dir) { // 文件夹 NSLog(@"文件夹"); // 4-1. 当前目录中全部资源 NSArray * arr = [nsfm contentsOfDirectoryAtPath:path error:nil]; // 4-2. 遍历当前目录里的全部资源 for (NSString * file in arr) { // 4-3. 目录内子资源的全路径 NSString * filePath = [NSString stringWithFormat:@"%@/%@", path, file]; // 4-4. 递归。计算子资源路径 codeLineCount(filePath); } } // 5. 如果是文件 else{ NSLog(@"文件"); // 5-1 过滤文件扩展名 NSString * extension = [path pathExtension]; // 转为小写,匹配 if (![[extension lowercaseString] isEqualToString:@"txt"] && ![[extension lowercaseString] isEqualToString:@"m"] && ![[extension lowercaseString] isEqualToString:@"h"] ) return; // 5-2 加载文本 NSString * content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; //NSLog(@%@, content); // 5-3 统计文本行数 // componentsSeparatedByString根据指定分割符拆分字串到数组 NSArray * array = [content componentsSeparatedByString:@"\n"]; NSLog(@"%@的文本行数:%d", path, array.count); } } int main(){ test(); codeLineCount(@"/Users/vigiles/Desktop/oc_workspace"); return 0; }
2.NSMutableArray可变数组
#import <Foundation/Foundation.h> int main(){ /** 可变数组创建 **/ NSMutableArray * mutArr1 = [NSMutableArray new]; NSMutableArray * mutArr2 = [NSMutableArray array]; NSMutableArray * mutArr3 = [[NSMutableArray alloc] init]; NSArray * nsarray = @[@"111", @"222", @"333"]; NSMutableArray * mutArr4 = [[NSMutableArray alloc] initWithArray:nsarray]; NSMutableArray * mutArr5 = [NSMutableArray arrayWithObjects:@"yyyy", @"eeee", @"ssss", nil]; /** 删除元素 **/ [mutArr5 removeObject:@"yyyy"]; [mutArr5 removeAllObjects]; /** 添加元素 **/ [mutArr4 addObject:@"4444"]; // 点方法获取数组长度-元素个数 mutArr4.count; // 消息指令获取元素个数 [mutArr4 count]; // 类方法根据索引获取元素 [mutArr4 objectAtIndex:2]; // C模式根据索引获取元素 mutArr4[0]; // 遍历元素 for (id obj in mutArr4) { // 获取元素的索引 [mutArr4 indexOfObject:obj]; NSLog(@"%@", obj); } // 使用Block遍历元素 [mutArr4 enumerateObjectsUsingBlock: // 每枚举一条记录,即更新id、idx、*stop ^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"%ld - %@", idx, obj); // 终止遍历 if (idx == 1) *stop = YES; // 注意:是*stop } ]; return 0; }
3.NSSet
#import <Foundation/Foundation.h> int main(){ // NSSet无序存储 NSSet * set1 = [NSSet set]; // 空 NSSet * set2 = [NSSet setWithObjects:@"aaaa", @"bbbb", @"cccc", nil]; // NSSet * set3 = [NSSet setWithArray:NSSArray]; // 根据已有的数组初始化 // NSSet * set4 = [NSSet setWithSet:[NSSet]; // 根据已有的Set初始化 NSString * str = [set2 anyObject]; // 随即返回对象 NSMutableSet * ms = [NSMutableSet set]; [ms addObject:@"可变Set"]; // 添加元素 [ms addObject:@"XXX"]; [ms removeObject:@"XXX"]; // 删除指定元素 [ms removeAllObjects]; // 删除全部元素 // 是集合,都能存放多个OC对象,且不能存放非OC对象 // 有可变子类MutableXXX // NSArray有顺序 // NSSet无序 }
4.NSDictionary字典
如同Java的Map。/** NSDictionary 字典 数据格式:键值对 根据key查找value **/ #import <Foundation/Foundation.h> int main() { // 创建方式 1 [NSDictionary 值 键] NSDictionary * dic = [NSDictionary dictionaryWithObject:@"值" forKey:@"键"]; // 两个数组中的数据一一对应 NSArray * keys = @[@"key.1", @"key.2", @"key.3", @"key.3"]; // 如果key重复,覆盖 NSArray * vals = @[@"val.1", @"val.2", @"val.3", @"val.3"]; // 创建方式 2 [NSDictionary 值数组 键数组] NSDictionary * dic2 = [NSDictionary dictionaryWithObjects:vals forKeys:keys]; // 创建方式 3 [NSDictionary 值,键, 值,键, 值,键] NSDictionary * dic3 = [NSDictionary dictionaryWithObjectsAndKeys: @"value01", @"key01", @"value02", @"key02", @"value02", @"key02", // 如果key重复,覆盖 @"value03", @"key03", nil]; // 创建方式 4 NSDictionary * dic4 = @{@"key-1":@"value-1", @"key-2":@"value-2", @"key-2":@"value-2", // 如果key重复,覆盖 @"key-3":@"value-3"}; // 读取方式 1 id value = [dic objectForKey:@"键"]; NSLog(@"键 - %@", value); // 读取方式 2 id val = dic2[@"key.1"]; NSLog(@"key.1 - %@", val); NSLog(@"dic,长度:%ld", [dic count]); // 键值对的数量 NSLog(@"dic2,长度:%ld", [dic2 count]); NSLog(@"dic3,长度:%ld", [dic3 count]); NSLog(@"dic4,长度:%ld", [dic4 count]); // 打印字典 NSLog(@"%@", dic4); /** { "key-1" = "value-1"; "key-2" = "value-2"; "key-3" = "value-3"; } **/ return 0; }
5.NSMutableDictionary可变字典
#import <Foundation/Foundation.h> int main() { // 创建方式 1 [NSDictionary 值 键] NSMutableDictionary * mutableDic = [NSMutableDictionary dictionary]; [mutableDic setObject:@"值" forKey:@"键"]; [mutableDic setObject:@"value" forKey:@"key"]; [mutableDic setObject:@"val" forKey:@"key"]; // key重复,覆盖 // 根据key删除条目 //[mutableDic removeObjectForKey:@"key"]; // 根据key,取value NSLog(@"“键 - %@”", mutableDic[@"键"]); // 打印字典 NSLog(@"全字典:%@", mutableDic); /** 常规方法遍历字典 **/ NSArray * arr = [mutableDic allKeys]; NSUInteger * integer = [mutableDic count]; for (int i = 0; i < integer; i ++) { // NSString * key = [arr objectAtIndex:i]; NSLog(@"%@ - %@", arr[i], mutableDic[arr[i]]); } /** 使用block代码块遍历字典 **/ [mutableDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { NSLog(@"%@ == %@", key, obj); }]; return 0; }
- end
相关文章推荐
- python-setuptools is needed by impala-shell-1.2.3-1.p0.352.el6.x86_64
- Linux系统怎么查看CPU是否支持虚拟化
- [LeetCode]Add Two Numbers
- Android Region代码分析
- yum安装gcc
- oracle异常
- ASP.NET中的STREAMREADER对象总结
- 大小端判断
- java调用分页存储过程
- 国产salesforce-------标准对象机能介绍
- 星型打分插件 bootstrap-rating-input
- NYOJ53不高兴的小明
- SPOJ 6285. Another Game With Numbers
- c#找不到类型或命名空间名称“Word”
- oracle分页
- HashMap使用注意
- C24_OC14-NSString字符串
- 读书笔记2014第4本:程序员修炼之道-从小工到专家(第七、八章)
- 九度 1206
- 开放-封闭原则