2017-10-04 92 views
0

我是swift和iOS開發中全新的。 我要初始化數組如何在swift中初始化數組

我這樣定義數組的時候我的課開始

var i = 0 
var ArrayTitle = [NSString]() 

,然後我需要初始化它的功能之一是這樣

let ArrayTitl[i] = "text" 

和我檢查它,即使這樣但錯誤是相同的

let ArrayTitl[0] = "text" 

但它有這個錯誤 Cannot assign to immutable expression of type '[Int]'

感謝任何幫助。謝謝

+5

有這麼多的問題,請閱讀[A斯威夫特巡迴賽(https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language /GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-ID1) – vadian

+2

歡迎來到斯威夫特!首先閱讀[語言指南](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html)。它會解釋這樣的基礎知識。 –

+1

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID109 –

回答

1

您正在使用動態數組,因此您需要附加值。

這裏有一個小爲例:

var arrayTitle = [String]() 

arrayTitle.append("text") 

print(arrayTitle[0]); 
+1

**不要**建議'NSString '和在Swift中以大寫字母開頭的變量名通過保留壞習慣 – vadian

+1

我剛剛複製了上面的代碼,但是,我會更正它! –