从扩展文件中检测设备 (Swift)

Detect device from extension file (Swift)

如何将我的代码放入 Extension 文件中以识别设备?

if UIDevice().userInterfaceIdiom == .pad {
        switch UIScreen.main.nativeBounds.height {
        case 2048:
            print("iPad Pro 9.7/Air")
        case 2224:
            print("iPad Pro 10.5")
        case 2732:
            print("iPad Pro 12.9")
            displayResultLabel.frame = CGRect(x: 2, y: 90, width: 370, height: 91)
            displayResultLabel.font = displayResultLabel.font.withSize(105)
        default:
            print("unknown")
        }
    }
extension THE_NAME_OF_CLASS_TO_EXTEND {

   func printDevice() {
        if UIDevice().userInterfaceIdiom == .pad {
            switch UIScreen.main.nativeBounds.height {
            case 2048:
                print("iPad Pro 9.7/Air")
            case 2224:
                print("iPad Pro 10.5")
            case 2732:
                print("iPad Pro 12.9")
                displayResultLabel.frame = CGRect(x: 2, y: 90, width: 370, height: 91)
                displayResultLabel.font = displayResultLabel.font.withSize(105)
            default:
                print("unknown")
            }
        }
    }
}

那么你可以使用这个:

THE_INSTANCE_OF_THE_CLASS_THAT_HAS_THE_NAME_OF_CLASS_TO_EXTEND.printDevice()

所以您只需要将该代码复制到一个新文件中。

THE_NAME_OF_CLASS_TO_EXTEND 替换为您的 class

并将 THE_INSTANCE_OF_THE_CLASS_THAT_HAS_THE_NAME_OF_CLASS_TO_EXTEND 替换为您的实例名称 class.. 这样就可以了 =]