OGeek|极客世界-中国程序员成长平台

标题: ios - NSLocale.current.description 在 Xcode 9 中崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:57
标题: ios - NSLocale.current.description 在 Xcode 9 中崩溃

我创建了一个全新的单 View 应用程序,并在 ViewController.swift 文件的 viewDidLoad 方法中添加了一行:

override func viewDidLoad() {
    super.viewDidLoad()
    _ = NSLocale.current.description
}

NSLocale.current.description 行在 Xcode 9 中崩溃,没有堆栈跟踪(只是 EXC_BAD_ACCESS code=EXC_I386_GPFLT 错误消息)。同一个项目在 Xcode 8.3.3 中运行良好。有人知道为什么会这样吗?

enter image description here

这是我的模拟器区域设置:

enter image description here



Best Answer-推荐答案


根据堆栈跟踪,它似乎试图将 description 视为 ObjC 属性而不是 Swift 属性。基于source code ,这不应该发生。这可能是生成 Swift 库的最新 Swift 编译器中的一个错误,因为它也会在 iOS 9 设备上崩溃。

请务必提交 bug with Swift因为这似乎是一个语言错误。我已经验证它在最新的 Swift 4 工具链中仍然存在问题。同时,您可以使用您自己的扩展来获得与 description 相同的行为,只需复制我在此处显示的预期实现即可。

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // let brokenDescription = NSLocale.current.description
        // let otherBrokenDescription = Locale.current.description

        let objcDescription = (Locale.current as NSLocale).debugDescription //"<__NSCFLocale: 0x1c00dbc10> \'en_US\'}"
        let myDescription = Locale.current.myDescription // "en_US (current)"
    }
}

extension Locale {
    private var _kindDescription : String {
        if self == Locale.autoupdatingCurrent {
            return "autoupdatingCurrent"
        } else if self == Locale.current {
            return "current"
        } else {
            return "fixed"
        }
    }

    public var myDescription: String {
        return "\(identifier) (\(_kindDescription))"
    }

    public var myDebugDescription : String {
        return "\(identifier) (\(_kindDescription))"
    }
}

关于ios - NSLocale.current.description 在 Xcode 9 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44768488/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4