On Sep 15, 2015, at 20:14 , Jim Thomason <[email protected]> wrote:
> 
> 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?


You can use a default image initially, as has been suggested, and that might be 
the simplest approach. Or, you can declare “type” as FooType! rather than 
FooType, which is a fairly standard thing to do in Swift with related objects 
whose lifetimes are not exactly identical.

When it’s of type FooType!, Swift will let you access the reference in property 
“type” without crashing. (You get a nil.) It would only crash in Swift if you 
then tried to dereference the reference in Swift code. However, if this 
property is accessed via a bindings, the access semantics follow Obj-C rules, 
which means that the binding won’t crash on a nil pointer. I haven’t actually 
tried this, so it’s something of a theory, but I think the overall effect is 
the same as the existing Obj-C behavior that’s generally tolerant of nil values.

The second part of the answer is KVO compliance. You need to ensure that 
establishing the Foo’s link to its FooType is done KVO compliantly for the 
“type” property. This may already be so, depending on how the relationship is 
established. KVO compliance is necessary for both of the above approaches.




_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to