property synthesize dynamic 的不同和区别

property:只是对getter和setter方法进行了声明,其他的什么也没有干。

synthesize:主要是对getter和 setter方法进行了实现,并且自动把你添加实例变量如果没有实例变量的话。实现主要是根据property特性的设置,例如property设置特性为 copy(retain)、readonly、assgin等。

dynamic:主要是告诉编译器说 不要给我生成 实例变量、getter、setter方法。我要自己生成。如果你没有自己生成当你在调用getter或者setter方法的时候,程序就会发生crash。

上代码

头文件中:

@interface Person : NSObject
{
    @private
    NSString *name;
    NSInteger age;
}

@property(nonatomic, copy) NSString *name;
@property(nonatomic, assign) NSInteger age;

(1)实现文件中:

@synthesize name;
@synthesize age;

实现文件中这样写。确实很完美。这样我们就很自然的生成 name、age 实例变量的getter 和setter方法。

(2)改变实文件中的写法

@synthesize name = _name;
@synthesize age;

在这里需要注意了,@synthesize name = _name; 这句话我们分为三部分来看。 第一部分:@synthesize:oc的关键字。第二部分:name:这里的name是指的property的后面那个变量的标识,如果我把头文件中的@property(nonatomic, copy)NSString *name;这句话中的“name” 改变为“nickName”,那么@synthesize 中的name 也应该相应的改变为nickName,否则就会出错。第三部分:_name,可能你会在这里迷惑,为什么会出来一个_name的标识呢?而且还没有出错。首先我们要明确的是这第三部分是是指实例变量。也就说那个实例变量要与property相对应。可能你会发现我的头文件中并没有_name的实例变量啊。这样难道不会报错吗?答案是不会的因为@synthsize 有自动创建实例变量的能力。如果编译器在实例变量列表中没有看找见_name 那么编译器就会自动的创建一个_name实例变量。这样我们在程序别的地方可以调用实力变量: _name = @"hello";这样是正确的。

(3)更改实现文件

@dynamic name;

如果这样写。就是说name的getter方法和setter方法没有实现。如果你在程序中调用  self setName: 这个方法会报错。

总结:

其实@Property 只是说明了 我们声明一个方法。而@synthesize 告诉我们 方法中的主角是谁,也就说要给那个实例变量赋值。

注意:

现在我们写程序可能只需要写@property (nonatomic, retain)NSString *nickName; 并没有发现什么@synthesize,那么是因为现在一句@property 相当做了两件事,一件事 他原本的含义声明getter和setter方法。一件是:@synthesize nickname= _nickname;所以我们现在实例变量都是加下划线的啊“_”.这是一个很好的编程规范。

时间: 2024-08-04 06:12:39

property synthesize dynamic 的不同和区别的相关文章

IOS 关键字self,super,copy, retain, assign , readonly , readwrite, nonatomic、 @synthesize、@property、@dynamic

IOS 关键字self,super,copy, retain, assign , readonly , readwrite, nonatomic.                     @synthesize.@property.@dynamic #synthesize关键字: 根据@property设置,自动生成成员变量相应的存取方法,从而可以使用点操作符来方便的存取该成员变量 . @implementation 关键字,表明类的实现 @end 结束 self 关键字 :类似于java中的t

@property @synthesize的含义以及误区。

@property的作用是定义属性,声明getter,setter方法.(注意:属性不是变量) @synthesize的作用是实现属性的,如getter,setter方法. 在声明属性的情况下如果重写setter,getter,方法,就需要把未识别的变量在@synthesize中定义,把属性的存取方法作用于变量.如: .h文件中 @property (nonatomic,assign)NSInteger age; @property (nonatomic,retain)NSString * na

Objective-C基础之@synthesize, @dynamic

Objective-C基础之@synthesize, @dynamic OC属性用一个表达式最能清楚的解释其实质:property = ivar + setter+getter,也就是说一个属性是由实例变量+setter方法+getter方法组成.那么现在编译器帮我们做了这件事,在很早之前的版本中需要我们手动合成,那么@synthesize property=ivar这种形式的合成.我们知道,什么情况下编译器不会自动给我们合成属性: 同时实现setter.getter方法的时候,系统不会再合成i

@property @synthesize 对着set方法和get方法

@property @synthesize 这两个的作用是代替set和get方法: @property int age;代替下面两句 - (void)setAge:(int)age; - (int)age; 作用: 自动生成某个成员变量的setter和getter声明: @property double weight; @property NSString *name; 现在需要到实现中去实现: 使用@synthesize age;会把age property这些声明进行实现: @synthes

OC语言@property @synthesize和id

一.@property @synthesize 这两个关键字是编译器特性,让xcode可以自动生成gettersetter的声明和实现. (一)@property  @property 关键字可以自动生成某个成员变量的settergetter方法的声明 @property int age; 编译时遇到这一行,则自动扩展成下面两句: - (void)setAge:(int)age; - (int)age; (二)@synthesize @synthesize关键字帮助生成成员变量的setterge

setter, getter, @property , @synthesize

一,单纯的set get, .h文件 @interface Person : NSObject { NSString *_name; } -(void)setName:(NSString *)name; -(NSString *)getName; @end .m文件 -(void)setName:(NSString *)name { _name = name; } -(NSString *)getName { return _name; } 调用 Person *person = [[Perso

object-c中@property @synthesize的用法

在objective-c中,我们可以用new简单的代替alloc init,我们今天介绍的是类似于new这种简易用法的另一种OC特性,用@property,@synthesize来代替get,set方法,用起来很简单,可以省掉很多的代码量,当需要用SET,GET方法的地方,我们可以用@property,@synthesize来简单的代替,这时系统会自动给我们生成该变量的set,get方法,@property对应方法的声明部分,@synthesize对应方法的实现部分. Human.h: [pla

成员变量的作用域,@property,@synthesize

成员变量的作用域 public:在任何地方都能直接访问对象的成员变量 private:只能在当前类的对象方法中直接访问(@implement中默认的) protected:可以在当前类及其子类的对象方法中直接访问(@interface中默认的) package:只要处在同一框架中,就能直接访问对象的成员变量 @property,@synthesize @property:可以自动生成某个成员变量的setter.getter方法: @synthesize:自动生成成员变量的setter.gette

「OC」@property @synthesize和id

一.@property @synthesize关键字 这两个关键字是编译器特性,让Xcode可以自动生成getter和setter. (一)@property 关键字 @property 关键字可以自动生成某个成员变量的setter和getter的声明 如:@property int age; 编译时遇到这一行,则自动扩展成下面两句: - (void)setAge:(int)age; - (int)age; (二)@synthesize关键字 @synthesize关键字帮助生成成员变量的set