devicekit / DeviceKit

DeviceKit is a value-type replacement of UIDevice.
MIT License
4.42k stars 428 forks source link

Unknown class in Interface Builder file when using Devicekit attribute on class #274

Closed olliekav closed 3 years ago

olliekav commented 3 years ago

Seeing a really weird bug since upgrading to Xcode 12 (12.2) and trying to run our app on iOS12, it works fine on iOS13 +.

Carthage DeviceKit -v 4.2.1

Any class where I define let device = Device.current early, crashes on iOS12. It will only work if I declare it within a function. e.g

This crashes with Unknown class _TtC11App21CustomViewController in Interface Builder file.

class CustomViewController: UIViewController {
        let device = Device.current
        override func viewDidLoad() {
               super.viewDidLoad()
               ......
        }
}

This works

class CustomViewController: UIViewController {
        override func viewDidLoad() {
               super.viewDidLoad()
               let device = Device.current
        }
}

I found this Stackoverflow comment that mentions why, but with no details how to fix it https://stackoverflow.com/a/64978875/1132441

Any ideas why this would be happening?

phimage commented 3 years ago

It seems weird in the first code a failure in current too soon will do not allow to instantiate the controller correctly

maybe first try to be kind with the compilator with typed value let device: Device = Device.current but I do not think it will change something because its runtime

so a mixed solution

class CustomViewController: UIViewController {
       var device: Device!
        override func viewDidLoad() {
               super.viewDidLoad()
               device = .current
        }
}
olliekav commented 3 years ago

@phimage many thanks, I'll try this out.