swift之Array

這幾天用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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末六剥,一起剝皮案震驚了整個濱河市晚顷,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌疗疟,老刑警劉巖该默,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異秃嗜,居然都是意外死亡权均,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進(jìn)店門锅锨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來叽赊,“玉大人,你說我怎么就攤上這事必搞”刂福” “怎么了?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵恕洲,是天一觀的道長塔橡。 經(jīng)常有香客問我梅割,道長,這世上最難降的妖魔是什么葛家? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任户辞,我火速辦了婚禮,結(jié)果婚禮上癞谒,老公的妹妹穿的比我還像新娘底燎。我一直安慰自己,他們只是感情好弹砚,可當(dāng)我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布双仍。 她就那樣靜靜地躺著,像睡著了一般桌吃。 火紅的嫁衣襯著肌膚如雪朱沃。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天茅诱,我揣著相機與錄音逗物,去河邊找鬼。 笑死瑟俭,一個胖子當(dāng)著我的面吹牛敬察,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播尔当,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼莲祸,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了椭迎?” 一聲冷哼從身側(cè)響起锐帜,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎畜号,沒想到半個月后缴阎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡简软,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年蛮拔,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片痹升。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡建炫,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出疼蛾,到底是詐尸還是另有隱情肛跌,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站衍慎,受9級特大地震影響转唉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜稳捆,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一赠法、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧乔夯,春花似錦期虾、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽喳坠。三九已至鞠评,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間壕鹉,已是汗流浹背剃幌。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留晾浴,地道東北人负乡。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像脊凰,于是被迫代替她去往敵國和親抖棘。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,509評論 0 23
  • 我一直都知道我是一個傾訴欲望的人狸涌,傾訴可以讓人覺得自己可以被人理解切省,但是生活中不是很多人都可以傾訴,所以我決定隨便...
    潘十七閱讀 252評論 0 1
  • 真正愛你的人帕胆, 會在你落魄的時候朝捆,不離不棄, 會在你失蹤的時候懒豹,焦躁不安芙盘。 真正愛你的人, 不會在你需要的時候脸秽,不...
    旋風(fēng)那個小土豆閱讀 281評論 0 3
  • 又過了千年儒老,紫霞終于再見到他。 “至尊寶记餐!” "孫...孫悟空贷盲?" 兩聲呼喚仿佛投進(jìn)了不見底的深窟,絲毫回響都看不...
    慵庸閱讀 417評論 0 0
  • 當(dāng)寫下這個標(biāo)題時,覺得是不是切入點太大了巩剖,因為自己并非全能需求大咖铝穷,也不是要寫專業(yè)的指導(dǎo)性文章,只是在讀完一篇推文...
    沈帕蒂閱讀 313評論 1 2