NSArray 迭代
NSObject *obj=[[NSObject alloc]init];
NSArray *array=[[NSArray alloc] initWithObjects:@"abc",obj,@"cde",@"opq",@25, nil];
//方法1 随便
//int i=0;
//int len=(int)array.count;
//for(;i<len;++i){
// NSLog(@"method1:index %i is %@",i,[array objectAtIndex:i]);
//}
/*结果:
method1:index 0 is abc
method1:index 1 is <NSObject: 0x100106de0> method1:index 2 is cde
method1:index 3 is opq
method1:index 4 is 25
*/
//方法2 好high
//for(id obj in array){
// NSLog(@"method2:index %zi is %@",[array indexOfObject:obj],obj);
//}
/*结果:
method2:index 0 is abc
method2:index 1 is <NSObject: 0x100602f00> method2:index 2 is cde
method2:index 3 is opq
method2:index 4 is 25
*/
//方法3,利用代码块方法 不知道
//[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// NSLog(@"method3:index %zi is %@",idx,obj);
// if(idx==2){//当idx=2时设置*stop为YES停止遍历
// *stop=YES;
// }
//}];
/*结果:
method3:index 0 is abc
method3:index 1 is <NSObject: 0x100106de0> method3:index 2 is cde
*/
//方法4,利用迭代器 推荐
//NSEnumerator *enumerator= [array objectEnumerator];//获得一个迭代器
NSEnumerator *enumerator=[array reverseObjectEnumerator];//获取一个反向迭代器 //
//NSLog(@"all:%@",[enumerator allObjects]);//获取所有迭代对象,注意调用完此方法迭代器就遍历完了,下面的nextObject就没有值了
id obj2=nil;
while (obj2=[enumerator nextObject]) {
if([obj2 length] > 2){//只是一个示例,可以加入条件进行选择
NSLog(@"method4:%@",obj2);
}
}
/*结果:
method4:25
method4:opq
method4:cde
method4:<NSObject: 0x100106de0> method4:abc
*/
NSArray 迭代的更多相关文章
- 遍历NSArray, NSDictionary, NSSet的方法总结
1,for循环读取 NSArray: NSArray *array = /*…*/ ; i<array.count; i++) { id object = array[i]; // do sth ...
- IOS - Objective-C NSArray和NSMutableArray的详解 使用
原文地址:http://blog.csdn.net/totogo2010/article/details/7729377 Objective-C的数组比C++,Java的数组强大在于,NSArray保 ...
- iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式 说明: 1)该文简短介绍在iOS开发中遍历字典.数组和集合的几种常见方式. 2)该文对应的代码可以在下面的地址获得:https:// ...
- iOS 学习 - 6.Objective-C中的各种遍历(迭代)方式
说明:转自文顶顶 一.使用 for 循环 要遍历字典.数组或者是集合,for 循环是最简单也用的比较多的方法 -(void)iteratorWithFor { //////////处理数组////// ...
- Objective-C语法之NSArray和NSMutableArray
转自:http://www.cnblogs.com/stoic/archive/2012/07/09/2582773.html Objective-C的数组比C++,Java的数组强大在于,NSArr ...
- ios开发 数据库版本迁移手动更新迭代和自动更新迭代
数据库版本迁移顾名思义就是在原有的数据库中更新数据库,数据库中的数据保持不变对表的增.删.该.查. 数据持久化存储: plist文件(属性列表) preference(偏好设置) NSKeyedArc ...
- Objective-C中NSArray和NSMutableArray是如何使用的?
Objective-C的数组比C++,Java的数组强大在于,NSArray保存的对象可以是不同的对象.但只能保存对象,int ,char,double等基本数据类型不能直接保存,需要通过转换成对象才 ...
- 用法总结:NSArray,NSSet,NSDictionary-备用
Foundation framework中用于收集cocoa对象(NSObject对象)的三种集合分别是: NSArray 用于对象有序集合(数组)NSSet 用于对象无序集合 (集合)NS ...
- 7、Objective-C中的各种遍历(迭代)方式
一.使用for循环 要遍历字典.数组或者是集合,for循环是最简单也用的比较多的方法,示例如下: //普通的for循环遍历 -(void)iteratorWithFor { //////////处理数 ...
随机推荐
- Oracle bbed使用说明1
一.centos上编译安装BBED工具 [orasrv@localhost ~]$ cd $ORACLE_HOME/rdbms/lib [orasrv@localhost ~]$ make -f in ...
- linux安装IPython四种方法
IPython是Python的交互式Shell,提供了代码自动补完,自动缩进,高亮显示,执行Shell命令等非常有用的特性.特别是它的代码补完功能,例如:在输入zlib.之后按下Tab键,IPytho ...
- django - transaction
def user_atomic(): User.objects.create(name='purk1', email='pwu1@maxprocessing.com') User.objects.cr ...
- ubuntu 下dbus的环境搭建和使用
从https://launchpad.net/ubuntu/+source/dbus/1.10.6-1ubuntu2下载需要的dbus包,然后解压,./configure make && ...
- QQ群里收集的外企iOS开发的笔试题
一组外企iOS开发的笔试题,您能回答出来吗?从群里收集来的. 1 why can't NSArray contain NSInteger Instance? with which extra step ...
- PHP中如何获取多个checkbox的值
> > > weeks后的中括号不可漏,否则用PHP获取的时候只能取到最后一个值.之后PHP就很好处理了,如下: PHP获取checkbox值方法一: $weeks = $_POST ...
- inputstream与其他格式的转换
1.InputStream 转换成InputSource . InputStream inputStream = request.getInputStream(); InputSource input ...
- startDiscovery() and startLeScan().
You have to start a scan for Classic Bluetooth devices with startDiscovery() and a scan for Bluetoot ...
- VBS基础篇 - 队列
VBS中的队列需要使用System.Collections.Queue '建立队列 Dim Que : Set Que = CreateObject("System.Collections. ...
- VS连接远程数据库,连接sqlserver2008,显示“基础提供程序在 Open 上失败”
今天安装完成VS2012后,在调试2010的程序的时候,出现“基础提供程序在 Open 上失败”,于是用vs连接远程sql2008,才发现问题是:“已成功与服务器连接,但是登录前的握手期间发生错误”, ...