21、objective-c数组的四种遍历方法总结
来源:互联网 发布:网络推广神器 编辑:程序博客网 时间:2024/05/22 10:33
objective-c 语言 数组遍历的4种方式:1、普通for循环;2、快速for循环;3、特性block方法;4、枚举方法。
Blog类:
#import "Blog.h"@implementation Blog +(Blog *)blog{ Blog * blog = [[Blog alloc] init]; return blog;} -(Blog *)setBlogTitle:(NSString *)title andContent:(NSString *)content{ _title = title; _content = content; return self;} -(NSString *)description{ return [NSString stringWithFormat:@"blog : title is \"%@\" , and content is \"%@\"", _title,_content ];} -(void)dealloc{ NSLog(@"%@被销毁了",self.title);}@end
主函数#pragma mark Array数组的四种遍历方法void testArray(){ Blog *blog1 = [[Blog blog] setBlogTitle:@"Love" andContent:@"I love you"]; Blog *blog2 = [[Blog blog] setBlogTitle:@"Friendship" andContent:@"you are my best friend"]; NSArray *array = [NSArray arrayWithObjects:@"hello",@"world",blog1,blog2, nil]; //第一种遍历:普通for循环 long int count = [array count]; for (int i = 0 ; i < count; i++) { NSLog(@"1遍历array: %zi-->%@",i,[array objectAtIndex:i]); } //第二种遍历:快速for循环,需要有外变量i int i = 0; for (id obj in array) { NSLog(@"2遍历array:%zi-->%@",i,[array objectAtIndex:i]); i++; } //第三种遍历:OC自带方法enumerateObjectsUsingBlock: //默认为正序遍历 [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"3遍历array:%zi-->%@",idx,obj); }]; //NSEnumerationReverse参数为倒序遍历 [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"4倒序遍历array:%zi-->%@",idx,obj); }]; //第四种遍历:利用枚举 NSEnumerator *en = [array objectEnumerator]; id obj; int j = 0 ; while (obj = [en nextObject]) { NSLog(@"5遍历array:%d-->%@",j,obj); j++; }}int main(int argc, const char * argv[]){ @autoreleasepool { testArray(); } return 0;}
0 0
- 21、objective-c数组的四种遍历方法总结
- objective-c数组的四种遍历方法总结
- objective-c数组的四种遍历方法总结
- objective-c数组的四种遍历方法总结
- objective-c数组的四种遍历方法总结
- Objective-C中,遍历数组的四种方法
- 黑马程序员——objective-c数组的四种遍历方法总结——黑马 ios 技术博客
- 黑马程序员------Objective-C------数组遍历方法总结
- iOS开发-Objective-C遍历数组NSArray的3种方法
- Objective-C的四种遍历collection的方式
- 遍历集合、数组的四种方法
- oc数组的四种遍历方法
- objective-c 语言 数组遍历的4种方式
- 遍历数组四种方法
- 数组的遍历和元素输出的四种方法
- OC数组的四种的遍历方法
- Objective C中数组排序几种情况的总结
- Objective C中数组排序几种情况的总结
- 山东中医药大学计算机科学与技术2班王鑫童第7章笔记
- Software enumeration using Internet Explorer
- 玩转Oracle服务器连接
- eclipse实现JavaWeb应用增量打包
- 漫谈程序员系列:一张图道尽程序员的出路 (转载,供自己看,感谢作者foruok)
- 21、objective-c数组的四种遍历方法总结
- 嵌入式安卓开发
- 最小生成树kruskal_征兵
- svn 本地仓库使用
- centos系统中Apache、php、mysql默认安装路径
- Android低版本使用ActionBar导入v7-appcompat出现的问题
- android 获取各种日期
- Java 中 properties文件的读取
- SQL Server表分区的详细讲解