PlankTon
PlankTon

Reputation: 12605

Objective C newbie: valueFor key works, .property doesn't

2-day objective-C newbie here with a Ruby/Python background.

I'm currently stumped by how properties work.

I have a class 'Person' with attributes 'personName' and 'raise'.

If I call

Person *newEmployee = [[Person alloc]init]; 
NSString *pn = [newEmployee valueForKey:@"personName"];
NSLog(@"%@", pn);

everything works just fine & dandy. But if I call

Person *newEmployee = [[Person alloc]init]; 
NSString *pn = [newEmployee.personName];
NSLog(@"%@", pn);

I get the following error:

error: request for member 'personName' in something not a structure or union

I was under the impression the two were equivalent. The person.m class has @synthesize personName in it, person.h has @property(readwrite, copy)NSString *personName;

Any suggestions most appreciated.

Upvotes: 0

Views: 49

Answers (2)

grahamparks
grahamparks

Reputation: 16296

Do you have #import "Person.h" at the start of the file that your code snippets are from?

Upvotes: 2

BoltClock
BoltClock

Reputation: 723398

When using the dot syntax to access properties you do not need the square brackets (unless you're sending a message to that property). Try this:

NSString *pn = newEmployee.personName;

Upvotes: 1

Related Questions