re: Dot syntax
re: Dot syntax
- Subject: re: Dot syntax
- From: Ben Trumbull <email@hidden>
- Date: Tue, 17 Jun 2008 21:15:36 -0700
I've been playing with Core Data and the syntax I use to access a Core
Data attribute. In Mac OS X 10.4 I used syntax like:
[a valueForKeyPath @"b.selection.c"]
where b is a NSObjectController bound to a Core Data entity with Cocoa
Bindings and c is an attribute of that entity. In Mac OS X 10.5 I'm
able to define
@property (retain) NSObjectController *b;
and now I can use syntax like:
[a.b.selection valueForKey @"c"]
Is there any way to take the last step so that I can use syntax like:
a.b.selection.c
to access the same attribute?
The . syntax is basically a uniform way of sending accessor methods.
-valueForKey: does that and more.
So
a.b.selection.c
is equivalent to:
[[[a b] selection] c]
but that is NOT identical to:
[a valueForKeyPath @"b.selection.c"]
under all circumstances. For example, if 'c' is '@count' or you've
overridden -valueForUndefinedKey:
Do I have to subclass NSObjectController and NSManagedObject?
If [[a b] selection] gives you back a single object that is an
instance of NSManagedObject, and 'c' is a property modeled by its
NSEntityDescription, then no, you don't need a subclass.
On 10.5, Core Data will ensure that any NSManagedObject will always
respond to accessor method invocations for any property defined in
its NSEntityDescription. Even if you do [[NSManagedObject alloc]
initWithEntity:entity insertIntoManagedObjectContext:moc], the object
you get back will handle any modeled properties' accessors methods.
If [[a b] selection] does not give you back an NSManagedObject
instance or if it does something different than [a valueForKeyPath
@"b.selection"] or if 'c' is not a modeled property, then the answer
is yes.
--
-Ben
_______________________________________________
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