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 (Cocoa-dev@lists.apple.com)

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

This email sent to [EMAIL PROTECTED]

Reply via email to