NSArray,NSSet,NSDictionary的遍历,基本使用集锦
2013-11-29 19:36
246 查看
NSArray *array = [NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangwu",@"zhaoda", nil];
//如何把 数组元素 一一取出?
//方法一:for循环
for (int i = 0; i < [array count]; i++) {
NSString *obj = [array objectAtIndex:i];
NSLog(@"%@",obj);
}
NSLog(@"----------");
//方法二:使用枚举器NSEnumerator
NSEnumerator *enumerator = [array reverseObjectEnumerator];
NSString *obj = nil;
while (obj = [enumerator nextObject]) {
NSLog(@"obj = %@",obj);
}
NSLog(@"----------");
//方法三:快速枚举 forin
for (NSString *obj in array) {
NSLog(@"%@",obj);
}
NSLog(@"----------");
//方法四:使用专业的枚举方式(blocks等)
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"!!!%@ %lu",obj,idx);
if(idx == 1)
{
*stop = YES;
}
}];
NSLog(@"#########");
//字典四种方法同上!
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"cui",@"name",@"18",@"age",@"nan",@"gender", nil];
//如何把 字典元素 一一取出?//字典没有下标概念 只能靠key区分对象
NSArray *allKeys = [dic allKeys];
NSArray *allValue = [dic allValues];
for(int i = 0;i < [dic count];i++){
NSString *key = [allKeys objectAtIndex:i];
//NSString *obj =[allValue objectAtIndex:i];
NSString *obj = [dic objectForKey:key];
NSLog(@"key = %@ obj = %@",key,obj);
}
NSEnumerator *en = [dic keyEnumerator];
NSString *key = nil;
while (key = [en nextObject]) {
NSLog(@"%@ = %@",key,[dic objectForKey:key]);
}
for (NSString *key in dic) {
NSLog(@"-%@--%@",key,[dic objectForKey:key]);
}
NSLog(@"++++++++++++");
NSSet *set = [NSSet setWithObjects:@"guangmu",@"duowen",@"zengchang",@"chiguo", nil];
//如何把 集合元素 一一取出?
NSArray *objects = [set allObjects];
for (int i = 0; i < [objects count]; i++) {
NSLog(@"%@",[objects objectAtIndex:i]);
}
NSEnumerator *e = [set objectEnumerator];
NSString *o = nil;
while (o = [e nextObject]) {
NSLog(@"%@",o);
}
for (NSString *obj in set) {
NSLog(@"=%@",obj);
}
}
//如何把 数组元素 一一取出?
//方法一:for循环
for (int i = 0; i < [array count]; i++) {
NSString *obj = [array objectAtIndex:i];
NSLog(@"%@",obj);
}
NSLog(@"----------");
//方法二:使用枚举器NSEnumerator
NSEnumerator *enumerator = [array reverseObjectEnumerator];
NSString *obj = nil;
while (obj = [enumerator nextObject]) {
NSLog(@"obj = %@",obj);
}
NSLog(@"----------");
//方法三:快速枚举 forin
for (NSString *obj in array) {
NSLog(@"%@",obj);
}
NSLog(@"----------");
//方法四:使用专业的枚举方式(blocks等)
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"!!!%@ %lu",obj,idx);
if(idx == 1)
{
*stop = YES;
}
}];
NSLog(@"#########");
//字典四种方法同上!
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"cui",@"name",@"18",@"age",@"nan",@"gender", nil];
//如何把 字典元素 一一取出?//字典没有下标概念 只能靠key区分对象
NSArray *allKeys = [dic allKeys];
NSArray *allValue = [dic allValues];
for(int i = 0;i < [dic count];i++){
NSString *key = [allKeys objectAtIndex:i];
//NSString *obj =[allValue objectAtIndex:i];
NSString *obj = [dic objectForKey:key];
NSLog(@"key = %@ obj = %@",key,obj);
}
NSEnumerator *en = [dic keyEnumerator];
NSString *key = nil;
while (key = [en nextObject]) {
NSLog(@"%@ = %@",key,[dic objectForKey:key]);
}
for (NSString *key in dic) {
NSLog(@"-%@--%@",key,[dic objectForKey:key]);
}
NSLog(@"++++++++++++");
NSSet *set = [NSSet setWithObjects:@"guangmu",@"duowen",@"zengchang",@"chiguo", nil];
//如何把 集合元素 一一取出?
NSArray *objects = [set allObjects];
for (int i = 0; i < [objects count]; i++) {
NSLog(@"%@",[objects objectAtIndex:i]);
}
NSEnumerator *e = [set objectEnumerator];
NSString *o = nil;
while (o = [e nextObject]) {
NSLog(@"%@",o);
}
for (NSString *obj in set) {
NSLog(@"=%@",obj);
}
}
相关文章推荐
- pyCharm 中有用的tip
- jQuery 学习笔记 遍历
- 3D跑酷 开发笔记(一)
- VS配置OpenCV
- MySQL基础
- node 加载内置函数
- sql学习(4张表引发的50个sql问题,提供表与数据的搭建)-- 连载一
- 当不想用正则时就自己动手"识文断字"
- 将html文件转换为pdf格式的文件
- android.9.png格式图片编辑
- jquery ajax无刷新翻页 分页
- LightOJ 1136 Division by 3 (想法题)
- poj 2923(状态压缩dp)
- 读取文本文件2
- android webview中的html代码 @media screen 在低版本系统下显示不正常的解决办法
- 关于解决博弈论问题的SG函数
- CF368D(处理数字出现次数)
- uva 10305
- HDU4681_String
- Function.apply()在提升程序性能方面的技巧