swift - RemoveAtIndex 使 Playground 崩溃

标签 swift

我正在尝试使用 StringremoveAtIndex 函数从字符串中删除 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/

相关文章:

class - Apple Swift - 在 arc4random_uniform 中放置一个变量

swift - 如何将自定义数据传递到 MTAudioProcessingTapProcessCallback?

ios - 更改 UIImageView 中 UIImage 的大小和位置(快速)

ios - Swift 如何重构从 API 获取数据?

ios - 将点更改为 UIPageViewController 的图像

swift - 在 Swift 中将 Array<Any> 转换为 Array<Int>

ios - 将文件从 bundle 复制到 Libraries 文件夹(IOS 10、Swift 3、xcode 8.2)

ios - 如何从 UIImageView 中删除边框/阴影?

swift - typealias 可以 Codable 吗?

ios - Swift 3 Firebase 更新代码转换 - 排序