这几天用swift发现应该仔细研究一下Array---于是找出了手册看了下,发现了一些东西
不知道从什么版本开始
Swift’sArray type now has full value semantics. Updated the information about Mutability of Collections and Arrays to reflect the new approach. Also clarified the Assignment and Copy Behavior for Strings, Arrays, and Dictionaries.
Array类型成为了数值类型,由结构来实现,所以在赋值或者作为参数传递的时候会拷贝一个全新的数值,而不像类一样,仅仅只是传递了一个引用 , 所以array也就没有了前面版本的unshare和copy方法
恩,就是这样的
然后
The value of an array includes the values of all of its elements. Copying an array also copies all of the elements of that array that are value types. This means that changing one of the elements of an array does not change the elements of any of the copies of the array. For example
意思就是由于Array变成了数值类型,当Array中某个元素被换掉之后,其copyArray是不会变的,保持原来对象
var array = [1, 2, 3]
var arrayCopy = array
array[0] = 100
// array is [100, 2, 3]
// arrayCopy is [1, 2, 3]
If the elements in an array are instances of classes, changing the class does affect other copies, because classes have reference semantics. For example:
class ExampleClass { var value = 10 }
var array = [ExampleClass(), ExampleClass()]
var arrayCopy = array
// Changing the class instance effects it in both places 改变数组元素中类的属性会影响COPY数组
array[0].value = 100
// arrayCopy[0].value is also 100
// Changing the elements of the array effects only one place 而改变数组的元素却不会
array[0] = ExampleClass()
// array[0].value is 10
// arrayCopy[0].value is 100
由于Arr变成了数值类型所以用for in循环是修改没法修改数组元素的,但是如果数组元素是类的话,可以修改类中的属性
var appleArr = [Apple(),Apple()]
for x:Apple in appleArr{
x.name = "modify"
}
print("\(appleArr[0].name)---\(appleArr[1].name)--") //modify
var strArr:[String] = ["a","b"]
for var x:String in strArr{
x = "modify"
}
print("\(strArr)---\(strArr)--") // [a,b]
Every array has a region of memory which stores the content of the array. If the array’s Element type is not a class or @objc protocol type, this storage is a contiguous block of memory; otherwise, this storage can be a contiguous block of memory, an instance of NSArray, or an instance of an NSArray subclass.
如果数组元素不是OBC的对象的话,那么其内存地址是连续的,反之,则有可能不是连续的
When an array’s storage is full, it allocates a larger region of memory and copies its elements into the new storage. The new storage is allocated to be a multiple of size of the old storage. This exponential growth strategy means that appending an element happens in constant time, averaging the performance of many append operations—append operations that trigger reallocation have a performance cost, but they occur less and less often as the array grows larger.
If you know approximately how many elements you will need to store, use the reserveCapacity(:) method before appending to the array to avoid the intermediate reallocations. Use the capacity and count properties to determine how many more elements the array can store without allocating a larger storage.
总的来说,append一个元素的时间是固定的,但是多个元素的添加代价是幂级数增长,增加元素意味着需要从新开辟更大的内存,swift将原来内存的元素复制过去,所以手册建议尽量在数组建立的时候就确定其长度,从而可以避免一些开销,如果要添加多个元素首先应该使用reserveCapacity(:)一次性分配好内存,可以避免中间的内存重新分配(因为如果多个元素依次添加的话,可能会经历很多次reallocation过程)
#不好的做法
var appendArr:[String] = ["c","d","e"]
var strArr:[String] = ["a","b"]
for var x in appendArr{
strArr.append(x)
}
print("\(strArr)")
#好的做法
var appendArr:[String] = ["c","d","e"]
var strArr:[String] = ["a","b"]
strArr.reserveCapacity(strArr.capacity+appendArr.capacity)
strArr.appendContentsOf(appendArr)
print("\(strArr)")
Bridging Between Array and NSArray
You can bridge between Array and NSArray using the as operator. All of the elements of the array must be bridged to Foundation types for the array to be bridged.
Bridging from Array to NSArray takes constant time and space if the array’s elements are instances of a class or an @objc protocol; otherwise, it takes linear time and space.
意思就是ARRAY和NSarray的转换 采用AS符号 ,如果元素是OBJ类实例的话会消耗固定时间,如果不是的话,会花费线性时间。
var strArr:[String] = ["a","b"]
let s = (strArr as NSArray).objectAtIndex(0)
print("\(s)")
Bridging from NSArray to Array first calls the copyWithZone: method on the array to get an immutable copy of the array, which takes linear time for most mutable subclasses of NSArray, and then performs additional Swift bookkeeping work, which takes constant time. However, if the instance of NSArray is already immutable, its implementation of copyWithZone: returns the same array in constant time. The instances of NSArray and Array share storage using the same copy-on-write optimization that is used when two instances of Array share storage.
从NSarray到Array的转换 会调用copyWithZone方法,获得一个不可变的数组,如果元素是可变的(obj类的实例)则会花费线性时间,如果元素是不可变的,则花费固定时间,,并且两个会共享存储空间。
The ContiguousArray class is not bridged; its instances always have a contiguous block of memory as their storage.
网友评论