Quantcast
Channel: Core Data – Michael Tsai
Viewing all articles
Browse latest Browse all 217

Clash of the Optionals

$
0
0

Tom Harrington:

What is an “optional” anyway? It depends who you ask. Swift will give you one answer, but Core Data has other ideas. So what happens when you bring them together?

[…]

Guess what, you just broke Swift’s initializer rules! Specifically, you initialized an object but didn’t provide a values for all of its non-optional properties. Nobody stopped you because @NSManaged says the rules don’t apply. You’ve just built a bomb.

[…]

Swift knows that initialization finished and there’s a non-optional property. It’s impossible for non-optional properties to be nil at this point, according to Swift. You can’t even check for nil values, that’s how definite this is. So it’s… not nil?

[…]

There is a way to check for nil first, but you have to pretty much forget that you’re using Swift first and fall back on key-value coding.

Making all your Core Data properties optional in Swift complicates the bulk of your code unnecessarily. But making them non-optional creates other problems, as above. I’ve also run into trouble with bridging changing nil to NSNull, which Core Data doesn’t like as a property value.

Previously:


Viewing all articles
Browse latest Browse all 217