Re: Accessing properties of a generic class
Re: Accessing properties of a generic class
- Subject: Re: Accessing properties of a generic class
- From: Chris Hanson <email@hidden>
- Date: Thu, 21 Aug 2008 00:33:22 -0700
On Aug 20, 2008, at 7:54 PM, John Murphy wrote:
I have a Person class with name and image properties stored in an
array. When I access its properties from within the controller class
like this:
Person *person = [objectArray objectAtIndex:0];
[nameField setStringValue:person.name];
[imageArea setImage:person.image];
everything works fine. But when I try to do it generically:
id object = [objectArray objectAtIndex:0];
[nameField setStringValue:object.name];
[imageArea setImage:object.image];
I get errors about the name and image properties not being in a
structure or union.
The compiler errors you get are expected. The Objective-C 2.0 dot
syntax is mapped to message sends just like bracket syntax is.
However, the mapping is more flexible: In the @property declaration
you can specify that a property has getter and setter methods with
specific names, or that it should be read-only and thus only have a
getter.
Because of this flexibility, the compiler needs additional compile-
time type information to know how to generate the actual message sends
that correspond to the property access.
For example, say I have a class representing a task:
@interface Task : NSObject {
@private
NSString *_title;
BOOL _completed;
}
@property(readwrite, copy) NSString *title;
@property(readwrite, assign, getter=isCompleted) BOOL completed;
@end
Now I want to log all completed tasks:
for (Task *task in self.allTasks)
if (task.completed) NSLog(@"Completed: %@", task.title);
The property name used is "completed" just as it is in the @property
declaration. However, it will be compiled identically to the
expression "[task isCompleted]" because that's what the property
actually means, since its declaration says "getter=isCompleted".
-- Chris
_______________________________________________
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