Objective-C中,遍历数组的四种方法
来源:互联网 发布:金融行业使用软件 编辑:程序博客网 时间:2024/05/21 15:48
在Objective-C中,博主总结了四种方法来遍历数组,以下是分享,希望有用。
#import <Foundation/Foundation.h>@interface Tire : NSObject@end@implementation Tire-(NSString *)description{ return @"I am a Tire";}@endint main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSMutableArray *array = [NSMutableArray arrayWithCapacity:40]; for(NSInteger i = 0;i < 4;i++) { Tire *tire = [Tire new];//创建类对象tire [array addObject:tire];//为可变数组array添加元素 } //1、通过索引遍历数组 for(NSInteger i = 0;i < [array count];i++) { NSLog(@"index %lu has %@",(long)i,[array objectAtIndex:i]);//结合计数和取值功能来输出数组内容(使用方法:-(id)objectAtIndex:(NSUInteger)index) } [array removeObjectAtIndex:1];//删除数组中索引为1的元素 for(NSInteger i = 0;i < [array count];i++) { NSLog(@"index %lu has %@",(long)i,array[i]);//使用数组字面量语法来获取数组元素 } //2、使用NSEnumerator遍历数组 NSEnumerator *enumerator = [array objectEnumerator]; id thingie; while(thingie = [enumerator nextObject]) { NSLog(@"I found %@",thingie); } //3、使用快速枚举遍历数组 NSString *object; for(object in array) { NSLog(@"%@",object); } //4、通过代码块方法遍历数组(-(void)enumerateObjectsUsingBlock:(void(^)(id obj,NSUInteger idx,BOOL *stop))block) [array enumerateObjectsUsingBlock:^(NSString *string,NSUInteger index,BOOL *stop){ NSLog(@"You found %@",string); }]; } return 0;}
以上就是博主的分享,有所补充希望读者也能提出,谢谢了。
0 0
- Objective-C中,遍历数组的四种方法
- objective-c数组的四种遍历方法总结
- objective-c数组的四种遍历方法总结
- 21、objective-c数组的四种遍历方法总结
- objective-c数组的四种遍历方法总结
- objective-c数组的四种遍历方法总结
- 黑马程序员——objective-c数组的四种遍历方法总结——黑马 ios 技术博客
- objective-C中数组的内存管理机制以及遍历
- iOS开发-Objective-C遍历数组NSArray的3种方法
- Objective-C的四种遍历collection的方式
- 遍历集合、数组的四种方法
- oc数组的四种遍历方法
- Objective-C中遍历字典、数组和集合的几种常见方式
- objective-c 语言 数组遍历的4种方式
- 遍历数组四种方法
- Objective-C中关于数组操作常用的方法
- 黑马程序员------Objective-C------数组遍历方法总结
- 数组的遍历和元素输出的四种方法
- Android4开发入门经典 之 第四部分:用户界面
- PHP基础
- 一台机器安装多个tomcat服务。Java heap space
- struts2导出excel笔记
- HTTP协议初探
- Objective-C中,遍历数组的四种方法
- linux实战(十一)----shell监控服务器性能---综合运用
- 运行时runtime
- C#中的委托和事件
- iOS 移除所有子视图
- 下载
- 反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
- android中xml tools属性详解
- HTTP协议(二):header标头说明