变量值导致错误:IR生成失败程序太聪明

时间:2016-02-27 23:14:20

标签: ios swift

以下是导致问题的简单代码:

class CoreViewController: UIViewController {
    private let isPad = UI_USER_INTERFACE_IDIOM() == .Pad
}

以下是erorr消息

  

< unknown&gt ;:0:错误:IR生成失败:程序太聪明:变量与现有符号碰撞OBJC_CLASS _ $ _ UIDevice

如果我删除UI_USER_INTERFACE_IDIOM() == .Pad

,则无误

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我相信UI_USER_INTERFACE_IDIOM是一个宏。它可能不适用于Swift。

答案 1 :(得分:2)

UI_USER_INTERFACE_IDIOM NOT 在Swift中工作,它是一个Objective-C宏。

解决方案

选项1。改为使用UIDevice.currentDevice().userInterfaceIdiom

switch UIDevice.currentDevice().userInterfaceIdiom {
    case .Phone:
        // It's an iPhone
    case .Pad:
        // It's an iPad
    case .Unspecified:
        // Undefined
    }

选项2。请求UITraitCollection个实例并检查成语(推荐)

let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom

switch (deviceIdiom) {

case .Pad:
        // It's an iPad
case .Phone:
        // It's an iPhone
case .TV: 
        // Apple TV
default:
        // Undefined    
}