黑马程序员------Objective-C------数组遍历方法总结
来源:互联网 发布:imf数据库使用指南 编辑:程序博客网 时间:2024/06/06 23:56
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
Blog类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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
主函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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
- 黑马程序员------Objective-C------数组遍历方法总结
- 黑马程序员——objective-c数组的四种遍历方法总结——黑马 ios 技术博客
- 黑马程序员------Objective-C------常见数组排序总结
- objective-c数组的四种遍历方法总结
- objective-c数组的四种遍历方法总结
- 21、objective-c数组的四种遍历方法总结
- objective-c数组的四种遍历方法总结
- objective-c数组的四种遍历方法总结
- 黑马程序员------Objective-C------NSString常用方法总结
- 【黑马程序员】---Objective-C核心语法总结
- 【黑马程序员】---Objective-C内存管理总结
- 黑马程序员---Objective-C 构造方法
- 黑马程序员---Objective-C Description方法
- 黑马程序员-----------Objective-C基础-----------类方法
- 黑马程序员——Objective-C学习之遍历字典
- 黑马程序员——Objective-C语言知识整理——构造方法相关知识总结
- 黑马程序员——Objective C中数组排序几种情况的总结——黑马 ios 技术博客
- 黑马程序员Objective-C笔记:对象方法,类方法
- PHP----DESTRUCT
- 黑马程序员------Objective-C------NSString常用方法总结
- linux下图片压缩出错,但是windows下正常
- 黑马程序员------Objective-C------常见数组排序总结
- DOM 浏览器支持情况
- 黑马程序员------Objective-C------数组遍历方法总结
- 基于jQuery的AJAX和JSON的实例[另附文章:深入浅出json]
- C# HttpWebRequest访问页面时自动识别编码
- Linux下用C语言读取配置文件
- 编码问题(UTF-8、gb2312、unicode)
- VMware下Linux网络配置攻略
- 设计模式—六大原则
- sublime text 3 实用快捷键总结
- 【开源下载】基于winform的xml菜单编辑器(c#源码)