2017-08-24 142 views
0

與NSMutableArray的工作,我有一個字符串的NSMutableArray skippedArray。在迅速

skipped Array = ["string1","string2","string3","string4"]; 

我想將索引0處的字符串分配給UILabel

我試圖lblQuestion.text = skippedArray[j] as! String ,但在這條線的應用程序崩潰。

誰能幫助?

+0

可以創建這樣的陣列,skippedArray:[字符串] = [ 「字符串1」, 「字符串2」, 「STRING3」 「串,4」];在這種情況下,不需要將值轉換爲String。 – Basheer

+2

什麼是崩潰?爲什麼你在swift中使用NSMutableArray?你應該只使用var arr = [] – 3stud1ant3

+0

你可以請分享崩潰報告。 –

回答

0

定義的NSMutableArray這樣的:

let skippedArray = ["string1","string2","string3","string4"]; 
lblQuestion.text = skippedArray.first 

或者您也可能是這樣的代碼:

let skippedArray : NSMutableArray =   ["string1","string2","string3","string4"]; 
lblQuestion.text = skippedArray.firstObject as! String 

還有另一種方法這樣抓取的NSMutableArray的對象:

let skippedArray : NSMutableArray = ["string1","string2","string3","string4"]; 
let j = 0; 
lblQuestion.text = skippedArray.object(at: j) as! String 
0

定義你的變量數組或乾脆[字符串]或只是讓定義其定義如下:

var skippedArray: Array<String> = ["string1","string2","string3","string4"] 

var skippedArray: [String] = ["string1","string2","string3","string4"] 

var skippedArray = ["string1","string2","string3","string4"] 
0

首先,定義可變數組正確,如

前面提到的
var skippedArray = ["string1","string2","string3","string4"] 

其次,定義您的索引,你稱之爲 「J」 與您的字符串

let j = 0 

三,設置標籤文本,從字符串數組

lblQuestion.text = skippedArray[j]