开发者

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

开发者 https://www.devze.com 2023-01-31 12:27 出处:网络
2-day objective-C newbie here with a Ruby/Python background. I\'m currently stumped by how properties work.

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, perso开发者_如何学运维n.h has @property(readwrite, copy)NSString *personName;

Any suggestions most appreciated.


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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消