From the developer that started to learn objective-c two days ago, I don't understand the difference between the following two methods :
1.
@interface Person : NSObject
@property NSString *firstName;
@property NSString *lastName;
@end
2.
@interface Person : NSObject{
NSString *firstName;
NSString *lastName;
}
@end
Using Java-Language,we define two String Field:
class Person extends Object{
(public/private/protected) String firstName;
(public/private/protected) String lastName;
}
I want to know which one(between 1 and 2) has same meaning with the Java code above.
Very thanks for @iamyogish @Popeye,if my answer is right i will correct both of you. After read the eBook: Objective-C 2.0 Essentials,I learned that(if it is not right,you tell me. ):
@interface Person : NSObject
@property NSString *firstName;
@property NSString *lastName;
@end
this is equivalent to the Java Code:
class Person extends Object{
private String firstName;
private String lastName;//In fact:private should be protected
//setter and getter.
//you can use ecplise tool to generate setter/getter method automaticly
}
As you can probably imagine, having to write these methods for large numbers(1000 or more.)of complex classes will ultimately prove to be time consuming. Objective-C provides synthesized accessor methods,so what you should is use of the @property and @synthesize directives.if you write your code like this:
@interface Person : NSObject
NSString *firstName;//note that the default access level is protected.
NSString *lastName;
@end
Unfortunatly, you need to provide methods that can access instance variables,such as(you can define the name of the function by yourself)
-(NSString ) getFirstName;
-(NSString ) getLastName;
-(void) setFirstName:(NSString * name);
-(void) setLastName:(NSString * name);
In addition to this,if the @property and @synthesize directives are used,you can access instance variables like C++/JAVA syntax dot notationsuch as:
NSString * firstName= [[Person alloc] init].firstName;
Note that: A key point to understand about dot notation is that it only works for instance variables for which synthesized accessor methods have been declared.
The access level for instance variables is specified in the @interface section of the class declaration using the @protected, @private and @public directives.
@interface Person : NSObject
@public
NSString *firstName;//note that the default access level is protected.
NSString *lastName;
@end
When accessing a public instance variable from another class or any other code in a methods or function, the -> pointer operator notation is used. So you can access the Public Filed in C++/C like:
[[Person alloc] init]->firstName = "your first name";
Another question is:
@interface Person : NSObject
@property NSString *firstName;
@property NSString *lastName;
@end
is is equivalent to:
@interface Person : NSObject{
NSString *firstName;
NSString *lastName;
}
@property NSString *firstName;
@property NSString *lastName;
@end
or not? and ivar
is equivalent to instance variable or not?