ios - Xcode 8.3.3 问题调用 String.index(of :)

标签 ios xcode8

当我尝试查找子字符串时,我的 Xcode 版本 8.3.3 (8E3004b) 崩溃了。如果我将以下内容粘贴到一个空的 Playground 上,它会崩溃:

import UIKit
let str = "abc"
str.index(of: "b")

我无法相信 Apple 会放过如此明显的错误,因此在谷歌搜索一番无果后,我认为问题出在我这边。但删除我的 Derived Data 文件夹并没有帮助。删除并重新安装 Xcode 没有帮助。

有趣的是,如果我删除“import UIKit”这一行,我会得到一个编译器错误而不是崩溃:

Playground execution failed: error: MyPlayground.playground:2:8: error: argument labels '(of:)' do not match any available overloads
   str.index(of: "b")
       ^    ~~~~~~~~~

MyPlayground.playground:2:8: note: overloads for 'index' exist with these partially matching parameter lists: (after: String.Index), (before: String.Index)
   str.index(of: "b")

   ^

所以编译器似乎找不到String中的index(of:)方法。但这是标准方法!我直接从 Apples 文档和“The Swift Programming Language”中复制粘贴示例,但得到了同样的错误。

我仍然无法想象这是 Xcode 中的一个错误,所以:

  • 还有什么我应该尝试的吗?
  • 这是一个已知错误吗?
  • 还有其他合理的方法吗(例如,我正在考虑在 Objective C 类中进行字符串处理以解决这个问题)?

崩溃详细信息(可能不重要)

当我从 Playground 查看崩溃日志时,崩溃似乎发生在 swift::TypeBase::getCanonicalType() + 23 中,同时试图从发生在swift::TypeChecker::solveForExpression

在我的项目中,是否导入库并不重要:我在编译时遇到了段错误。这是我的测试类:

class Bug {
    func foo() {
        let str = "abc"
        let _ = str.index(of: "b")
    }
}

我收到一个 Swift 编译器错误,“由于信号导致命令失败:段错误:11”。 swift::TypeChecker::solveForExpression() + 6229 中发生了一些事情,导致调用 swift::constraints::ConstraintSystem::salvage,最终错误发生在 swift::TypeBase::getContextSubstitutions(swift::DeclContext const*) + 1158

最佳答案

似乎 String.index(of:) 在 Swift 4 中是新的,我应该调用 String.characters.index(of:)String.range(of:) 代替。我忘记了该函数的名称并进行了快速的网络搜索,但 Apple 似乎删除了 Swift 3 的所有文档(即使他们尚未发布支持 Swift 4 的 IDE)。方法名称非常相似。

我猜这次崩溃是由于 UIKit 中 index(of:) 的部分过早实现造成的,而我在 playground 中得到的编译器错误实际上是正确的。

关于ios - Xcode 8.3.3 问题调用 String.index(of :),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45636538/

相关文章:

ios - 将[[((Int,String)]]]组合成[(Int,String)]的优雅方法?

ios - UILabel 的宽度超过使用编程自动布局的文本

Xcode 8 : The request to open bundle-id failed. SpringBoard 无法为请求提供服务

ios - Swift 可选协议(protocol)行为的不一致

ios - 获取应用程序文件ios xcode 7/8

ios - 有没有办法使用 Appcelerator Titanium 修改 iOS webView 中的上下文菜单?

iOS 如何限制 ASIHTTPRequest 并发连接数?

ios - 如何将字符串发送到另一个 View Controller

ios - 如何做我的 appDelegate coredata 支持 ios 9 我正在使用 xCode 8

ios - 更新 swift 版本和 pod 后的链接器错误