objective-c数组的四种遍历方法总结
来源:互联网 发布:mac怎么安装office软件 编辑:程序博客网 时间:2024/06/14 20:10
Blog类:
01
#import "Blog.h"
02
@implementation Blog
03
04
+(Blog *)blog{
05
Blog * blog = [[Blog alloc] init];
06
return
blog;
07
}
08
09
-(Blog *)setBlogTitle:(NSString *)title andContent:(NSString *)content{
10
_title = title;
11
_content = content;
12
return
self;
13
}
14
15
-(NSString *)description{
16
return
[NSString stringWithFormat:@
"blog : title is \"%@\" , and content is \"%@\""
, _title,_content ];
17
}
18
19
-(
void
)dealloc{
20
NSLog(@
"%@被销毁了"
,self.title);
21
}
22
@end
主函数:
01
#pragma mark Array数组的四种遍历方法
02
void
testArray(){
03
Blog *blog1 = [[Blog blog] setBlogTitle:@
"Love"
andContent:@
"I love you"
];
04
Blog *blog2 = [[Blog blog] setBlogTitle:@
"Friendship"
andContent:@
"you are my best friend"
];
05
NSArray *array = [NSArray arrayWithObjects:@
"hello"
,@
"world"
,blog1,blog2, nil];
06
07
//第一种遍历:普通for循环
08
long
int
count = [array count];
09
for
(
int
i = 0 ; i < count; i++) {
10
NSLog(@
"1遍历array: %zi-->%@"
,i,[array objectAtIndex:i]);
11
}
12
13
//第二种遍历:快速for循环,需要有外变量i
14
int
i = 0;
15
for
(id obj in array) {
16
NSLog(@
"2遍历array:%zi-->%@"
,i,[array objectAtIndex:i]);
17
i++;
18
}
19
20
//第三种遍历:OC自带方法enumerateObjectsUsingBlock:
21
22
//默认为正序遍历
23
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx,
BOOL
*stop) {
24
NSLog(@
"3遍历array:%zi-->%@"
,idx,obj);
25
}];
26
//NSEnumerationReverse参数为倒序遍历
27
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx,
BOOL
*stop) {
28
NSLog(@
"4倒序遍历array:%zi-->%@"
,idx,obj);
29
}];
30
31
//第四种遍历:利用枚举
32
NSEnumerator *en = [array objectEnumerator];
33
id obj;
34
int
j = 0 ;
35
while
(obj = [en nextObject]) {
36
NSLog(@
"5遍历array:%d-->%@"
,j,obj);
37
j++;
38
}
39
}
40
int
main(
int
argc,
const
char
* argv[])
41
{
42
@autoreleasepool {
43
testArray();
44
}
45
return
0;
46
}
结果:
0 0
- objective-c数组的四种遍历方法总结
- objective-c数组的四种遍历方法总结
- 21、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中数组排序几种情况的总结
- Visual C++常用数据类型转换详解
- 第十三章 带参方法 上机练习4 模拟账户查询,实现取款取款
- 我在Github上安家啦!
- RtlZeroMemory中的Rtl是什么意思
- [纠错]fatal error C1853: 'Debug/Park.pch' is not a precompiled header file created
- objective-c数组的四种遍历方法总结
- justep X5入门笔记
- java.net.BindException: Address already in use: bind
- 【Leetcode】ZigZag Conversion
- mybatis操作oracle数据库主键自增(触发器)
- C/C++ 初始化几种方式
- MdiTabControl
- 表视图
- MATLAB 定义结构体数组 并存到mat 文件 读取mat