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 { //////////处理数 ...
随机推荐
- TDirectory.CreateDirectory 完整、严谨的创建一个目录
描述:创建一个目录,不包含多级目录(多级目录使用System.SysUtils.ForceDirectories,Vcl.FileCtrl.ForceDirectories已过时) procedure ...
- 转发 python中file和open有什么区别
python中file和open有什么区别?2008-04-15 11:30地痞小流氓 | 分类:python | 浏览3426次python中file和open有什么区别?都是打开文件,说的越详细越 ...
- python学习笔记30(全局变量的两种解决办法)
先看程序: >>> count = 0 >>> def fuc(count): print count count +=1 >>> for i i ...
- LintCode-Word Search II
Given a matrix of lower alphabets and a dictionary. Find all words in the dictionary that can be fou ...
- JSTL标签总结
一.JSTL简介: 1.JSP标准标签库JSTL(JSP Standard Tag Library)是一个JSP标签集合,它封装了JSP应用的通用核心功能. 2.JSTL支持通用的.结构化的任务.比如 ...
- <a href='?out=login'>是什么意思
<a href='?out=login'>退出</a>前面加上问号?就是GET方式传递out=login是要传递的数据点这个链接就可以执行 接下来通过$_GET["o ...
- python re 正则表达式[转]
01.#!/usr/bin/env python 02.# -*- coding: utf-8 -*- 03.import re 04. 05.def regex(): 06. str = 'abcd ...
- mysql安装篇
装了vps,环境又要自己配置.mysql比较难装上. 1.安装MySQL 5.5.x的yum源:rpm -Uvh http://repo.webtatic.com/yum/centos/5/lates ...
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp
题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...
- 【BZOJ】【2818】Gcd
欧拉函数/莫比乌斯函数 嗯……跟2190很像的一道题,在上道题的基础上我们很容易就想到先求出gcd(x,y)==1的组,然后再让x*=prime[i],y*=prime[i]这样它们的最大公约数就是p ...