我正在尝试使用 String
的 removeAtIndex
函数从字符串中删除 Character
。但这会让 Playground 崩溃。
完整代码如下:
let string = "Some String"
let start = advance(string.startIndex, 2)
let x = string.removeAtIndex(start)
最佳答案
您已使用 let
声明了 string
,这使得它不可变。由于您只能在 mutable String
上调用 removeAtIndex
,因此您可以通过使用 声明
改为:string
来解决此问题var
var string = "Some String"
let start = advance(string.startIndex, 2)
let x = string.removeAtIndex(start)
注意:以上代码在 Xcode 6.1 中有效,但在 Xcode 6.0 中会使编译器崩溃。
对于 Xcode 6.0,可以使用全局 removeAtIndex
函数:
var string = "Some String"
let start = advance(string.startIndex, 2)
let x = removeAtIndex(&string, start)
关于swift - RemoveAtIndex 使 Playground 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26446299/