UI_USER_INTERFACE_IDIOM()崩溃了仅在设备上分发的应用程序

Ash*_*hok 1 crash ios swift

我在viewDidLoad()中创建了一个基于swift语言的示例单页基于swift语言的应用程序CRASHES

func regularFont() -> UIFont {
    var fontSize : CGFloat = (UI_USER_INTERFACE_IDIOM() == .Pad) ? 15 : 12
    return UIFont.systemFontOfSize(fontSize)
}
Run Code Online (Sandbox Code Playgroud)

但是一旦我用Apple推荐的UIDevice()方法替换了这个UI_USER_INTERFACE_IDIOM(),它就可以了.

func regularFont() -> UIFont {
    var fontSize : CGFloat = (UIDevice().userInterfaceIdiom == .Pad) ? 15 : 12
    return UIFont.systemFontOfSize(fontSize)
}
Run Code Online (Sandbox Code Playgroud)

崩溃发生在我测试的所有设备上 - iPhone 5s,iPhone 6和iPad Air(全部在iOS 8.x上),它只发生在设备上,而不是模拟器上.

PS:该应用程序通过我们的OTA/Web链接安装在所有设备上.

令我惊讶的是,我们在Apple的App Store上有另一个基于c语言的客观应用,它大量使用UI_USER_INTERFACE_IDIOM(),定期更新......但由于这个原因从未崩溃.

有什么想法吗?

Ngh*_*ong 5

UI_USER_INTERFACE_IDIOM() 只是一个Objective-C宏,定义为:

#define UI_USER_INTERFACE_IDIOM() \ ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \ [[UIDevice currentDevice] userInterfaceIdiom] : \ UIUserInterfaceIdiomPhone)
Run Code Online (Sandbox Code Playgroud)

因此,当您使用Swift时,您需要使用:

UIDevice.currentDevice().userInterfaceIdiom == .Pad
UIDevice.currentDevice().userInterfaceIdiom == .Phone
UIDevice.currentDevice().userInterfaceIdiom == .Unspecified
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.