iOS 中的可遍数组NSMutableArray
2016-05-06 14:16
337 查看
创建NSMutableArray可变数组
向数组中添加元素
在数组中删除元素
替换数组中某个位置的元素
交换数组中某两个指定位置的元素
1 //对象方法创建可变数组 2 NSMutableArray *array1 = [[NSMutableArray alloc] init]; 3 //类方法创建可变数组 4 NSMutableArray *array2 = [NSMutableArray arrayWithCapacity:0];
向数组中添加元素
1 NSMutableArray *array = [[NSMutableArray alloc] init]; 2 NSArray *tmp = [NSArray arrayWithObjects:@"jing", @"dian", @"ying", @"xue", @"yuan", nil]; 3 [array addObject:@"bei"]; 4 [array addObjectsFromArray:tmp]; 5 //- (void)insertObject:(id)anObject atIndex:(NSUInteger)index; 6 //作用:数组中任意一个位置插入元素 7 [array insertObject:@"hao" atIndex:1]; 8 NSLog(@"%@", array);
在数组中删除元素
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"nan", @"jing", @"hao", nil]; #if 0 //- (void)removeObjectAtIndex:(NSUInteger)index; //作用:删除数组中指定下标的元素 [array removeObjectAtIndex:2]; #endif #if 0 //- (void)removeObject:(id)anObject; //作用:删除数组中指定的元素(有多少删多少) [array removeObject:@"hao"]; #endif #if 0 //- (void)removeObjectsInRange:(NSRange)range; //作用:从某位置起,删除某长度的元素 [array removeObjectsInRange:{1, 2}]; #endif //- (void)removeAllObjects; //作用:删除数字中的全部元素 [array removeAllObjects]; NSLog(@"%@", array);
替换数组中某个位置的元素
1 NSMutableArray *array = [NSMutableArray arrayWithObjects:@"nan", @"jing", @"hao", nil]; 2 //- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject; 3 //作用:替换数组中指定下标的元素 4 [array replaceObjectAtIndex:0 withObject:@"bei"];
交换数组中某两个指定位置的元素
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"nan", @"jing", @"hao", nil]; //- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2; //作用:交换数组中指定下标的两个元素 [array exchangeObjectAtIndex:0 withObjectAtIndex:2];
相关文章推荐
- iOS开发系列-线程问题
- IOS开发: NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]
- 如何用 Instruments 检测 iOS 每个进程的电量消耗?
- iOS滑动解锁、滑动获取验证码效果的实现代码
- iOS自动处理键盘事件的第三方库:IQKeyboardManager
- iOS的几种后台机制
- IOS CopyPNGFile 异常问题解决
- iOS开发系列-动画绘图CALayer
- iOS 关于已配对设备重新检测连接问题
- iOS开发系列--打造自己的“美图秀秀”
- iOS开发-进阶:语言国际化及代码切换语言
- iOS开发_修改系统cell的图像会变模糊的处理
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
- IOS应用内存释放机制
- iOS 日期转成几年月日时分秒前
- iOS 日期格式转几年几月几日几时几分几秒前
- iOS---使用MJExtension 归档
- iOS端播放VR图片
- iOS开发系列--视图切换
- iOS开发系列--无限循环的图片浏览器