Should all CoreData relationships be optional in swift?
Should all CoreData relationships be optional in swift?
- Subject: Should all CoreData relationships be optional in swift?
- From: Jim Thomason <email@hidden>
- Date: Tue, 15 Sep 2015 22:14:06 -0500
I'm doing some porting/updating work and came across an issue.
I have two objects - which we'll call Foo and FooType.
Every Foo must have a FooType, and every FooType is associated with
multiple Foos.
The FooType also has an image associated with it, and the Foo uses that
image exclusively.
So I added this:
class Foo {
...
var image : NSImage { get { return self.type.image } }
}
The problem is, this image value is used to populate a cell in a Source
Control List. When I add a new Foo, it's immediately added to the table.
And it pops up in there before a FooType is associated with it.
That in turn means that the image getter tries to go through self.type,
which isn't defined yet.
How do I fix this? I can't do self.type?.image or if let t = self.type,
because the type is declared as non-optional and I can't unwrap an optional
value.
Presumably, I can just make the type optional, but it's not -really-
optional, since all Foos _must_ have a type before they're saved. It's a
mandatory relationship.
Generalizing wildly, I can see this issue popping up with any relationship
- if you have a derived property that relies upon a relationship that may
not have been set yet, you'd fail with an error. So should all
relationships just be made optional to get around this? That seems like the
wrong approach.
Any suggestions?
-Jim....
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden