Reading info.plist: it is assumed to be of type AnyObject, which may be unexpected

I pasted integerinto my file info.plistand obviously would like to get it from my Swift UIViewController. The code is very simple:

class MyController: UIViewController {

    private let value: Int

    required init(coder aDecoder: NSCoder) {

        if let info = NSBundle.mainBundle().objectForInfoDictionaryKey("v"){
            value = info.integerValue
        }else{
            value = 0
        }

        super.init(coder: aDecoder)
    }

    //other auto-generated-methods
    //...

}

The function objectForInfoDictionaryKeyreturns AnyObject?optional, and I'm fine with that. But the compiler tells me that

Permanent "information" that is of type AnyObject, which may be Unexpected

How can I resolve this warning and why should I worry about the function returning AnyObject? <is a question mark ... lol

Even if it returns AnyObject, I can disable it if the object is not nil. I do not understand why the compiler complains about this.

+4
1

Xcode , AnyObject AnyObject?. ( Xcode), :

if let info : AnyObject = ...

, Xcode , "" , info , , , .

, info.integerValue , , . , NSNumber NSString info.integerValue .

, :

if let info = NSBundle.mainBundle().objectForInfoDictionaryKey("v") as? NSNumber {
    value = info.integerValue
} else {
    value = 0
}

NSNumber Int

if let info = NSBundle.mainBundle().objectForInfoDictionaryKey("v") as? Int {
    value = info
} else {
    value = 0
}

nil-coalescing ??:

let value = NSBundle.mainBundle().objectForInfoDictionaryKey("v") as? Int ?? 0
+12

Source: https://habr.com/ru/post/1569896/


All Articles