如何更改Xcode Playground上的区域设置

时间:2015-06-26 05:52:46

标签: ios xcode swift localization locale

我想更改Xcode Playground上的区域设置以测试本地化。

我找到了这个解决方案,但它不适用于Xcode 6.3.2 Playground: http://natashatherobot.com/locale-playground-swift/

1 个答案:

答案 0 :(得分:12)

哦,我找到了解决方案!

extension NSLocale {
    class func mono_currentLocale() -> NSLocale {
        return NSLocale(localeIdentifier: "fr")
    }
}
let original = class_getClassMethod(NSLocale.self, #selector(getter: NSLocale.currentLocale))
let swizzled = class_getClassMethod(NSLocale.self, #selector(NSLocale.mono_currentLocale))
method_exchangeImplementations(original, swizzled)

编辑:Swift 4.1版本:

extension NSLocale {
    @objc class func mono_currentLocale() -> NSLocale {
        return NSLocale(localeIdentifier: "fr")
    }
}
let original = class_getClassMethod(NSLocale.self, #selector(getter: NSLocale.current))!
let swizzled = class_getClassMethod(NSLocale.self, #selector(NSLocale.mono_currentLocale))!
method_exchangeImplementations(original, swizzled)