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

mon*_*ono 8 xcode locale localization ios swift

我想更改Xcode Playground上的语言环境来测试本地化.

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

mon*_*ono 13

哦,我找到了解决方案!

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)
Run Code Online (Sandbox Code Playgroud)

编辑: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)
Run Code Online (Sandbox Code Playgroud)


iUr*_*rii 5

XCode 13 / Swift 5

\n

要更改游乐场中的当前区域设置,您可以创建扩展名NSLocale并覆盖currentLocale

\n
extension NSLocale {\n    @objc\n    static let currentLocale = NSLocale(localeIdentifier: "en_GB") // Set a needed locale\n}\n\nlet formatter = NumberFormatter()\nformatter.numberStyle = .currency\nlet text = formatter.string(from: 12345.67 as NSNumber)!\nprint(text)\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
\xc2\xa312,345.67\n
Run Code Online (Sandbox Code Playgroud)\n
\n

注意:它也适用于单元测试。

\n
\n