What is the Objective-C property? What is the difference between a property and an instance variable?

I am very confused between instance variables and property. I read the number of posts about this, but still I do not quite understand. I am from the JAVA background, and what I am doing from the C object documentation is that the property is similar to the JAVA BEAN CLASS (one of which has getter and setter instances of varibles). The property can be obtained from other classes using the getter and setter methods, while the instance variable is private and other classes cannot be accessed.

I think in that direction?

+3
source share
7 answers

Java . , Objective C , , Java - . , Objective C , Java .

" " . ( ) , , .

+4

, . - , - assecor (getter/setter) ( ).

, . . , - @property(nonatomic, assign, readwrite) NSString *name; ,

-(NSString *)name;
-(void)setName:(NSString *)name;

.

  • - (.m), , , .

  • - , , synthesize name;. .

, ;)

+2

, ... , , , - BIG PICTURE...

. , ivars ... ivar , . , ( ivar), , getter/setter. . , , ivar.

+2

c - , , setter getter, . , , ,

@interface example:NSObject{
    NSString *variable;
}
@property(readwrite, assign) NSString *variable;
@end

@implementation
@synthesize variable;
@end

-(NSString *)getVariable;
-(void)setVariable(NSString *)value;

, ,

instance.variable = something;
something = instance.variable;
+1

, getter/setter. :

@property (nonatomic) int value;

:

-(void)setValue:(int)i
{
    value = i;
}
-(int)value
{
    return self->value;
}

@synthesize d.

Objective-C 1.0, , . 2.0, . - getter/setter.

, , , . , -(void)setValue:(int)i . , , value.

Java, Objective-C @public, . Java getter/setter. Objective-C getter/setter .

, http://cocoacast.com/?q=node/103 Objective-C 2.0, .

+1

, , , . , . , .

@interface person:NSObject{
    NSDate *birthDate;
}
@property(readonly) int age;
@end

@implementation

-(int) age{
 // return calculated age based on birthDate and today;
}
@end

.

@synthesize myProperty = myVar;
+1

All Articles