我正在读“
Swift编程语言
Swift 4.2“和”子串“部分下的”字符串和字符“一章中,以下代码为例:
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)
我将这个块复制并粘贴到我的Xcode游乐场;但是,我收到以下错误:
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()的方法.
我有进口UIKit;在我操场的顶部.
你能让我知道为什么我会收到这个错误吗?
你可以尝试索引(
let index = greeting.index(of: ",") ?? greeting.endindex
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。