美文网首页
Objective-C中的@property、@synthesi

Objective-C中的@property、@synthesi

作者: chjxidian | 来源:发表于2020-03-15 11:23 被阅读0次

Objective-C中的@property@synthesize及点语法都是与两个函数有关的,一是setter函数另一个是getter函数
以前我们是这样来定义setter与getter函数的

@interface Dog:NSObject
{
    int age;
}
-(void)setAge:(int)newAge;
-(void)age;
@end

@implementation Dog
-(void)setAge:(int)newAge
{
    age = newAge;
}
-(void)age
{
    return age;
}
@end

而如今我们有了更好的方法:
@property是让编译器自动产生setter与getter的函数声明
@sythesize就是让编译器自动实现setter与getter函数
现如今我们这样写就可以了

@interface Dog:NSObject
{
    int age;
}
@property int age;
@end

@implementation Dog
@synthesize age;
}
@end

当@properpty后是非基本数据类型时,可以加参数,有关点语法:

dog.age = 10;
dogAge = [dog ahe];

编译器会把dog.age = 10;展开成【dog setAge:10];
会把dogAge = dog.age;展开成dogAge = [dog age];
点语法在等号的左边,它就是一个setter函数,点语法在等号的右边就是一个getter函数。

  • 参考一下前辈们的总结:
    声明property的语法为 :@property (参数1,参数2) 类型 名字;
    @property(nonatomic,retain) UIWindow *window;
  • 其中参数主要分为三类:
读写属性: (readwrite/readonly)
setter语意:(assign/retain/copy)
原子性: (atomicity/nonatomic)
  • 各参数意义如下:
readwrite: 产生setter\getter方法
readonly: 只产生简单的getter,没有setter。
assign: 默认类型,setter方法直接赋值,而不进行retain操作
retain: setter方法对参数进行release旧值,再retain新值。
copy: setter方法进行Copy操作,与retain一样
nonatomic: 禁止多线程,变量保护,提高性能

参数类型

参数中比较复杂的是retain和copy,具体分析如 下:

  • getter 分析
@property(nonatomic,retain)test* thetest;  
@property(nonatomic ,copy)test* thetest;
等效代码:
-(void)thetest  
{  
  return thetest;  
}
@property(retain)test* thetest;  
@property(copy)test* thetest;
等效代码:
-(void)thetest  
{  
    [thetest retain];  
    return [thetest autorelease];  
}
  • setter分析
@property(nonatomic,retain)test* thetest;  
@property(retain)test* thetest;
等效于:
-(void)setThetest:(test *)newThetest {  
    if (thetest!= newThetest) {  
        [thetestrelease];  
        thetest= [newThetest retain];  
    }  
}
@property(nonatomic,copy)test* thetest;  
@property(copy)test* thetest;
等效于:
-(void)setThetest:(test *)newThetest {  
    if (thetest!= newThetest) {  
        [thetest release];  
        thetest= [newThetest copy];  
nonatomic

如果使用多线程,有时会出现两个线程互相等待对方导致锁死的情况(具体可以搜下线程方面的注意事项去了解)。在没有(nonatomic)的情况下,即默认(atomic),会防止这种线程互斥出现,但是会消耗一定的资源。所以如果不是多线程的程序,打上(nonatomic)即可retain

  • 代码说明
    如果只是@property NSString*str; 则通过@synthesize自动生成的setter代码为:
-(void)setStr:(NSString*)value{  
    str=value;  
}
如果是@property(retain)NSString*str; 则自动的setter内容为:
-(void)setStr:(NSString*)v{  
    if(v!=str){  
        [str release];  
        str=[v retain];  
    }  
}

在这个看到的一个比较纠结的地方
@synthesize window=_window;意思是说,window 属性为_window 实例变量合成访问器方法。 也就是说,window属性生成存取方法是setWindow,这个setWindow方法就是_window变量的存取方法,它操作的就是_window这个变量。
下面是一个常见的例子

@interface MyClass:NSObject{ 
MyObjecct *_myObject; 
} 
@property(nonamtic, retain) MyObjecct *myObject; 
@end 
@implementatin MyClass 
@synthesize myObject=_myObject; 

这个类中声明了一个变量_myObject,又声明了一个属性叫myObject,然后用@synthesize生成了属性myObject的存取方法,这个存取方法的名字应该是:setmyObject和getmyObject@synthesize myObject=_myObject的含义就是属性myObject的存取方法是做用于_myObject这个变量的。
这种用法在Apple的Sample Code中很常见

相关文章

网友评论

      本文标题:Objective-C中的@property、@synthesi

      本文链接:https://www.haomeiwen.com/subject/yeiqehtx.html