gpt4 book ai didi

Swift 4.2,Xcode Playground 中的 String firstIndex() 函数错误

转载 作者:搜寻专家 更新时间:2023-10-30 23:07:41 24 4
gpt4 key购买 nike

我在读《The Swift Programming Language》Swift 4.2”和“子字符串”部分下的“字符串和字符”一章中,给出了以下代码作为示例:

let greeting = "Hello, world!"
let index = greeting.firstIndex(of: ",") ?? greeting.endIndex
let beginning = greeting[..<index]
// beginning is "Hello"

// Convert the result to a String for long-term storage.
let newString = String(beginning)

我将这个 block 复制并粘贴到我的 Xcode playground 中;但是,我收到以下错误:

Playground execution failed:

error: MyPlayground.playground:6:13: error: value of type 'String' has
no member 'firstIndex'
let index = greeting.firstIndex(of: ",") ?? greeting.endIndex
^~~~~~~~ ~~~~~~~~~~

我检查了 https://developer.apple.com/documentation/swift/string 上的 String 类确实有一个名为 firstIndex() 的方法.

我在 playground 的顶部有 import UIKit;

您能告诉我为什么会出现此错误吗?

最佳答案

你可以试试index(of

let index = greeting.index(of: ",") ?? greeting.endIndex

因为 firstIndex 存在于 Xcode 10 beta Doc

关于Swift 4.2,Xcode Playground 中的 String firstIndex() 函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51596406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com