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;
精彩评论