Warpzit Warpzit - 9 days ago 5
Objective-C Question

Swift and objective-C userdefault boolean issue

So I'm saving a boolean to userdefaults in my main app (objective-c) and then I read the value (through group) in my swift extension. If I read the value like following it works:

if let isKey = userDefaults.object(forKey: Constants.GroupShares.isKey) as? Bool {
if !isKey {
// awesome
}
}


But if I read it like following it will always be false:

let isKey = userDefaults.bool(forKey: Constants.GroupShares.isKey)


I'd imagine that swift and objective-c booleans are fully compatible but this makes we wonder if I'm misunderstanding something?

Also the solution that works is simply too many lines of codes. If that is the only solution I'd prefer to simply use an object and use that instead.

Answer

I have no real answer. But you could do the same in one line:

let isKey = (userDefaults.object(forKey: Constants.GroupShares.isKey) as? Bool) ?? false