在swift中使用NSMutableArray

时间:2017-08-24 10:19:34

标签: ios swift nsmutablearray

我有一个NSMutableArray skippedArray of strings。

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

我想将索引0处的字符串分配给UILabel

我试过lblQuestion.text = skippedArray[j] as! String 但该应用程序在这一行崩溃。

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

将变量定义为Array或简单地[String]或者只是让定义按如下方式定义:

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

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

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

答案 1 :(得分:0)

首先,正确定义Mutable Array,如前所述

count = 0   # to check how many '->' are there in each line
for sentence in lines:
    for letter in sentence:
        if letter == '-':
            #check if the next character is '>'   (How to code this line)
            #and if so, increment count
        else:
            break

其次,定义你的索引,你的名字叫&#34; j&#34;

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

第三,用字符串数组

设置标签文本
let j = 0

答案 2 :(得分: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