Array与NSArray关系
2017-05-01 21:43
155 查看
NSArray与Array之间的关系如同NSString与String之间的关系,NSArray是类类型,而Array是结构体类型,一个是引用类型,一个是值类型,它们是怎样实现无缝转换的呢?Swift在底层能够将它们自动地桥接起来,一个NSArray对象桥接之后的结果是[AnyObject]数组(保存AnyObject元素的Array数组)。
下面我们来看一个使用Array和NSArray的例子
1 | let ocStr : NSString = "Swift is easy" 2 | let strArray : NSArray = ocStr.components(separatedBy: " ") 3 | let swiftArray = strArray 4 | for item in strArray { 5 | print(item) //输出类型是NSString 6 | } 7 | for item in strArray as! [String] { 8 | print(item) //输出类型是String 9 | } 10 | for item in swiftArray { 11 | print(item) //输出类型是AnyObject 12 | } 13 | for item in swiftArray as! [String]{ 14 | print(item) //输出类型是String 15 | } |
第4行代码是遍历strArray集合,第5行代码输出的是NSString数据。第7行代码是将数组strArray通过as!转换为[String]数组,然后遍历集合,第8行代码输出的是String类型的数据。第11行代码输出的是AnyObject数据。第14代码输出的是String数据。
相关文章推荐
- NSArray 跟 NSMutableArray 使用 区别
- Search in Rotated Sorted Array II
- 【leetcode】Array——Jump Game(55)
- 238. Product of Array Except Self
- java System.arrayCopy 参数意义,使用
- Array.isArray()
- Leetcode:Remove Duplicates from Sorted Array
- 【Leetcode】之Find Minimum in Rotated Sorted Array
- Leetcode——80. Remove Duplicates from Sorted Array II
- codeforces 315 B.Sereja and Array(线段树区间更新+单点更新+单点询问)
- 撞墙吧 Array.IndexOf(xxx)
- 【Android】JSONArray的合并
- [leetcode]Merge Sorted Array (两个有序数组的合并 C语言实现)
- Java源码阅读之ArrayDeque
- 26. Remove Duplicates from Sorted Array
- 167. Two Sum II - Input array is sorted
- 浅尝boost之array
- jcharArray to const char[]
- **(leetcode_Array)Rotate Array
- Variably modified array at file scope