這幾天用swift發(fā)現(xiàn)應(yīng)該仔細(xì)研究一下Array---于是找出了手冊看了下厌处,發(fā)現(xiàn)了一些東西
不知道從什么版本開始
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類型成為了數(shù)值類型捉片,由結(jié)構(gòu)來實現(xiàn)巴刻,所以在賦值或者作為參數(shù)傳遞的時候會拷貝一個全新的數(shù)值赤屋,而不像類一樣肤京,僅僅只是傳遞了一個引用 , 所以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變成了數(shù)值類型蹈垢,當(dāng)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 改變數(shù)組元素中類的屬性會影響COPY數(shù)組
array[0].value = 100
// arrayCopy[0].value is also 100
// Changing the elements of the array effects only one place 而改變數(shù)組的元素卻不會
array[0] = ExampleClass()
// array[0].value is 10
// arrayCopy[0].value is 100
由于Arr變成了數(shù)值類型所以用for in循環(huán)是修改沒法修改數(shù)組元素的,但是如果數(shù)組元素是類的話燎猛,可以修改類中的屬性
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.
如果數(shù)組元素不是OBC的對象的話叼丑,那么其內(nèi)存地址是連續(xù)的,反之扛门,則有可能不是連續(xù)的
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一個元素的時間是固定的纵寝,但是多個元素的添加代價是冪級數(shù)增長论寨,增加元素意味著需要從新開辟更大的內(nèi)存,swift將原來內(nèi)存的元素復(fù)制過去爽茴,所以手冊建議盡量在數(shù)組建立的時候就確定其長度葬凳,從而可以避免一些開銷,如果要添加多個元素首先應(yīng)該使用reserveCapacity(:)一次性分配好內(nèi)存室奏,可以避免中間的內(nèi)存重新分配(因為如果多個元素依次添加的話火焰,可能會經(jīng)歷很多次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的轉(zhuǎn)換 采用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的轉(zhuǎn)換 會調(diào)用copyWithZone方法绒怨,獲得一個不可變的數(shù)組,如果元素是可變的(obj類的實例)則會花費線性時間纯赎,如果元素是不可變的,則花費固定時間南蹂,犬金,并且兩個會共享存儲空間。
The ContiguousArray class is not bridged; its instances always have a contiguous block of memory as their storage.