我想用文本拆分创建一个数组,然后删除我想要的任何项目。代码片段没有编译,因为a不可变。 我该怎么办?
var a = []
// ...
a = split(text, { $0 == "\n"}, maxSplit: Int.max, allowEmptySlices: false)
let sentence = a.removeAtIndex(0)
答案 0 :(得分:4)
var a = []
将a
定义为NSArray
,但没有removeAtIndex()
。取而代之
与
var a : [String]
应该解决问题。或者,一步声明并分配变量:
var a = split(text, { $0 == "\n"}, maxSplit: Int.max, allowEmptySlices: false)
答案 1 :(得分:0)
修改数组声明?
var a = Array<String>()
let text = "this has \nsome words"
a = split(text, { $0 == "\n"}, maxSplit: Int.max, allowEmptySlices: false)
let sentence = a.removeAtIndex (0)
print(a)
打印“[some words]”