ios developer tiny share-20161021
来源:互联网 发布:weiapibridge.js 编辑:程序博客网 时间:2024/06/04 18:29
今天讲Objective-C的可变数组,之前讲的NSArray是不可变的,immutable,今天讲NSMutableArray,它是可变的。
Mutability
Although the NSArray class itself is immutable, this has no bearing on any collected objects. If you add a mutable string to an immutable array, for example, like this:
NSMutableString *mutableString = [NSMutableString stringWithString:@"Hello"];NSArray *immutableArray = @[mutableString];
there’s nothing to stop you from mutating the string:
if ([immutableArray count] > 0) {id string = immutableArray[0];if ([string isKindOfClass:[NSMutableString class]]) {[string appendString:@" World!"];}}
If you need to be able to add or remove objects from an array after initial creation, you’ll need to use NSMutableArray, which adds a variety of methods to add , remove or replace one or more objects:
NSMutableArray *mutableArray = [NSMutableArray array];[mutableArray addObject:@"gamma"];[mutableArray addObject:@"alpha"];[mutableArray addObject:@"beta"];[mutableArray replaceObjectAtIndex:0 withObject:@"epsilon"];
This example creates an array that ends up with the objects @"epsilon", @"alpha", @"beta".
It’s also possible to sort a mutable array in place, without creating a secondary array:
[mutableArray sortUsingSelector:@selector(caseInsensitiveCompare:)];
In this case the contained items will be sorted into the ascending, case insensitive order of @"alpha", @"beta", @"epsilon".
0 0
- ios developer tiny share-20161021
- ios developer tiny share-20160628
- ios developer tiny share-20160629
- ios developer tiny share-20160630
- ios developer tiny share-20160701
- ios developer tiny share-20160704
- ios developer tiny share-20160705
- ios developer tiny share-20160706
- ios developer tiny share-20160707
- ios developer tiny share-20160708
- ios developer tiny share-20160711
- ios developer tiny share-20160712
- ios developer tiny share-20160713
- ios developer tiny share-20160714
- ios developer tiny share-20160715
- ios developer tiny share-20160718
- ios developer tiny share-20160719
- ios developer tiny share-20160720
- iOS-各种加密/解密算法
- 错排递推式推导
- 【VS开发】组播(多播)的C程序实战
- Android NDK开发篇(一):新版NDK环境搭建(免Cygwin,超级快)
- 靠“喂喂喂”来测试实时语音质量靠谱吗?
- ios developer tiny share-20161021
- Error configuring application listener of class org.springframework.web.context.ContextLoaderListene
- MySQL (2)数据的完整性
- Haroopad--最好用的markdown编辑器
- 项目中的关于UIDatePicker
- scala学习-类与对象
- Jenkins 常见问题
- Android Intent安全性检查
- python 迭代器