NSMutableArray常见用法
2015-11-18 22:41
399 查看
#import <Foundation/Foundation.h> @interface Person : NSObject @end @implementation Person @end int main(int argc, const char * argv[]) { @autoreleasepool { /* 数组可以包括任意类型 */ Person *p = [[Person alloc] init]; NSMutableArray *arr1 = [[NSMutableArray alloc] initWithObjects: @"One String", @"Two", @"Three", p, nil]; /* 数组中的每个对象实际是该对象的地址 */ /* NSLOG打印时,调用的是对象的description方法,字符串返回本身,对象为多个C类型字符串则返回时包含双引号 */ NSLog(@"arr1-%@", arr1); /* 利用已知数组初始化新数组 */ NSMutableArray *arr2 = [[NSMutableArray alloc] initWithArray: arr1]; /* 利用已知数组初始化时的地址浅拷贝 */ NSMutableArray *arr3 = [[NSMutableArray alloc] initWithArray: arr1 copyItems: NO]; /* 生成空的数组后追加 */ NSMutableArray *arr4 = [[NSMutableArray alloc] init]; [arr4 addObject:@"First String"]; [arr4 addObject:@"Second"]; /* 使用枚举器遍历数组 */ NSEnumerator *enumarator = [arr2 objectEnumerator]; id anObject; while (anObject = [enumarator nextObject]) { NSLog(@"arr2 object-%@", anObject); } /* 快速遍历法 */ for (anObject in arr3) { NSLog(@"arr3 object--%@", anObject); } /* i遍历法 */ NSInteger i = 0; for ( ; i < [arr4 count]; i++) { NSLog(@"arr4 object---%@", [arr4 objectAtIndex:i]); } /* 删除部分或全部元素 */ [arr2 removeObject:@"Two"]; NSLog(@"arr2 is %@", arr2); [arr2 removeAllObjects]; NSLog(@"arr2 is %@", arr2); [arr4 removeObject:@"First String"]; NSLog(@"arr4 is %@", arr4); /* 元素交换 */ [arr3 exchangeObjectAtIndex:0 withObjectAtIndex: 2]; NSLog(@"arr3 is %@", arr3); /* 注意M.a.c输出时以字符串形式 */ NSString *str1 = @"This is my M.a.c book"; NSArray *arr5 = [str1 componentsSeparatedByString: @" "]; NSLog(@"arr5 is %@", arr5); /* 逆序 */ NSMutableArray *arr6 = [[NSMutableArray alloc] init]; NSEnumerator *enumra = [arr5 reverseObjectEnumerator]; while (anObject = [enumra nextObject]) { [arr6 addObject: anObject]; } NSLog(@"arr6 is %@", arr6); /* 拼接成字符串 */ NSMutableString *str2 = [[NSMutableString alloc] init]; id obj; for (obj in arr6) { [str2 appendFormat:@"%@ ", obj]; } NSLog(@"str2 is %@", str2); } return 0; }
输出结果:
2015-11-18 22:39:53.637 TestMutableArray[646:18692] arr1-( "One String", Two, Three, "<Person: 0x100206a00>" ) 2015-11-18 22:39:53.641 TestMutableArray[646:18692] arr2 object-One String 2015-11-18 22:39:53.641 TestMutableArray[646:18692] arr2 object-Two 2015-11-18 22:39:53.641 TestMutableArray[646:18692] arr2 object-Three 2015-11-18 22:39:53.641 TestMutableArray[646:18692] arr2 object-<Person: 0x100206a00> 2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr3 object--One String 2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr3 object--Two 2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr3 object--Three 2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr3 object--<Person: 0x100206a00> 2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr4 object---First String 2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr4 object---Second 2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr2 is ( "One String", Three, "<Person: 0x100206a00>" ) 2015-11-18 22:39:53.643 TestMutableArray[646:18692] arr2 is ( ) 2015-11-18 22:39:53.643 TestMutableArray[646:18692] arr4 is ( Second ) 2015-11-18 22:39:53.643 TestMutableArray[646:18692] arr3 is ( Three, Two, "One String", "<Person: 0x100206a00>" ) 2015-11-18 22:39:53.643 TestMutableArray[646:18692] arr5 is ( This, is, my, "M.a.c", book ) 2015-11-18 22:39:53.643 TestMutableArray[646:18692] arr6 is ( book, "M.a.c", my, is, This ) 2015-11-18 22:39:53.644 TestMutableArray[646:18692] str2 is book M.a.c my is This
相关文章推荐
- UI第七天 UIScrollView UIPageControl
- 使用Bundle传递复杂数据
- 练习PYTHON之EPOLL
- android学习ViewPager的简单使用
- AngularJs angular.Module模块接口配置
- LeetCode 31 Next Permutation(下一个排列)
- 关于Vim
- AngularJs搭建项目
- ios开发在Xcode中如何给程序添加icon图标
- 互联网思维下运营商的服务变革
- Android快速开发系列 10个常用工具类
- ActiveMQ 中的消息持久化(一)
- Ubuntu 12.04 安装配置 Apache2
- Linux____ftp
- 网站如何做到完全不需要使用jQuery
- android SQLiteOpenHelper的使用方法
- 初次使用图片搜索
- 堆排序
- 21.UIScrollView(滑动视图)和UIPageControl()
- JAVA 日期加减