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 { //////////处理数 ...
随机推荐
- Highcharts条形与柱形同时显示
var chart; $(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'chart_combo' //关联页面元素di ...
- 深入理解JavaScript的变量作用域(转载Rain Man之作)
在学习JavaScript的变量作用域之前,我们应当明确几点: JavaScript的变量作用域是基于其特有的作用域链的. JavaScript没有块级作用域. 函数中声明的变量在整个函数中都有定义. ...
- 每日一“酷”之copy
Copy – 复制对象 作用:提供一些函数,可以使用浅副本或深副本语义复制对象. copy模块包括两个函数copy()和deepcopy(),用于复制现有的对象 1. 浅副本 copy()创建的浅副 ...
- 11G RAC 简单命令
1.查看集群状态: [root@rac1 ~]# su - grid [grid@rac1 ~]$ crsctl check clusterCRS-4537: Cluster Ready Servic ...
- Python - python不是内部或外部命令
[方法一]我的电脑->属性->高级->环境变量->系统变量 在系统变量里找到PATH,双击PATH,在结尾加上 ";C:\Python26"(不要引号) ...
- 精灵的属性Zorder的设置
1.Zorder是CCSprite从父类CCNode那继承来的protected属性: class CCNode{ protected: int m_nZOrder; ...
- JGibbLDA:java版本的LDA(Latent Dirichlet Allocation)实现、修改及使用
转载自:http://blog.csdn.net/memray/article/details/16810763 一.概述 JGibbLDA是一个java版本的LDA(Latent Dirichl ...
- C#中的委托与事件 笔记
1.委托是类型安全的回调函数,是将方法作为方法参数.委托可以注册多个方法:委托就是一个 multicastdelegate类,可以通过=赋值,+=添加方法(对象方法与静态方法),内部使用Delega ...
- Sponsored Feature: Common Performance Issues in Game Programming
转自http://www.gamasutra.com/view/feature/132084/sponsored_feature_common_.php?print=1 By Becky Heinem ...
- tomcat 优化实记
好记性不如烂笔头,以下是 tomcat 无脑优化 1.内存设置(VM参数调优)(1). Windows环境下,是tomcat解压版(执行startup.bat启动tomcat) 解决办法:修改“%T ...