#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];
long
int
count = [array count];
for
(
int
i = 0 ; i < count; i++) {
NSLog(@
"1遍历array: %zi-->%@"
,i,[array objectAtIndex:i]);
}
int
i = 0;
for
(id obj in array) {
NSLog(@
"2遍历array:%zi-->%@"
,i,[array objectAtIndex:i]);
i++;
}
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx,
BOOL
*stop) {
NSLog(@
"3遍历array:%zi-->%@"
,idx,obj);
}];
[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;
}