`
h416756139
  • 浏览: 359392 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

@synthesize

    博客分类:
  • ios
 
阅读更多

声明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 分析

  1、 @property(nonatomic,retain)test* thetest;
  @property(nonatomic ,copy)test* thetest;

  等效代码:
  -(void)thetest
  {
  return thetest;
  } 

  2、@property(retain)test* thetest;
  @property(copy)test* thetest; 
  等效代码:
  -(void)thetest 
  { 
   [thetest retain]; 
  return [thetest autorelease];
  } 

setter分析
  1、   @property(nonatomic,retain)test* thetest;
  @property(retain)test* thetest;

  等效于:
  -(void)setThetest:(test *)newThetest {
  if (thetest!= newThetest) {
   [thetestrelease];
  thetest= [newThetest retain];
  } 
  } 
   2、@property(nonatomic,copy)test* thetest; 
  @property(copy)test* thetest; 
  等效于:
  -(void)setThetest:(test *)newThetest {
  if (thetest!= newThetest) {
  [thetestrelease];
  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];
}
}

分享到:
评论

相关推荐

    Objective-C中的@property和@synthesize用法详解

    相信每个初学者对@property和@synthesize都感到非常的陌生,在此给大家分享下我的自己的理解,有不当之处,还望多多指教。详细说明文章在下面连接http://blog.csdn.net/comeontom/article/details/7455459

    Objective-c解析XML封装

    1 获取XML的数据DATA 2 调用解析类,提供对象名。 3 返回NSMutableDictionary或者NSMutableArray,键值封装。...@synthesize currentResult; @synthesize map; @synthesize list; ********0..........

    Objective-c对象组装XML

    @synthesize objectName; @synthesize lvUp; @synthesize root; @synthesize buildEnd; -(NSData *)objctPackage:(NSMutableDictionary *)object objectName:(NSString *)name xmlTemplateName:(NSString *)...

    objective-c小技巧

    1. 使用@property和@synthesize声明一个成员变量,给其赋值是时要在前面加上"self.",以便调用成员变量的setmember方法。 直接调用成员变量并且给其赋值:member=[NSString stringWithFormat:@””];将不执行...

    StoryboardTest

    用Xcode4.5编写,突出新的性能。如:@synthesize by default(属性自动绑定)等。

    irrelon-synthesize:一个用于 JavaScript 模块的简单 getter setter 合成器

    自动创建简单的方法来获取和设置 JavaScript 模块的属性,类似于 iOS Objective-C 中的 @synthesize 指令。 用法 在这个例子中,我们在 MyClass 原型上创建了一个名为 name() 的 getter/setter 方法: var MyClass...

    传智博客-Objective-C PPT

    本套PPT,拥有以下OC语言的内容: ...二、OC的特有语法(点语法、@property、@synthesize关键字、id、构造方法、分类、description、SEL) 三、内存管理 四、协议(protocol)代码块(block) 五、ARC 等等

    varidict_handler

    变量处理程序一个ObjC助手来使用varidict处理程序。 一行代码值得一千个单词...@implementation Consumer@synthesize handler = _handler;- ( void ) setHandler : (NDHandler) handler { static Validator validator

    react-native-hybrid-app-examples:在现有iOS应用程序中使用React Native的示例集合

    AppDelegate.m @synthesize bridge;...- ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions{ ... configure the jsCodeLocation here...

    iOS开发中属性 property 和 synthesize 详解

    针对iOS开发中属性 property 和 synthesize 进行了详细介绍

    利用iOS绘制图片生成随机验证码示例代码

    先来看看效果图 实现方法 .h文件 @property (nonatomic, retain) NSArray *changeArray; @property (nonatomic, retain)...@synthesize changeArray = _changeArray; @synthesize changeString = _changeString; @sy

    引导程序:针对高质量编码的iOS项目引导程序

    iOS项目引导您如何设置iOS项目?...代码质量和警告警告是由编译器团队添加的原因,例如,我从Weverything开始并禁用一些警告: Wno-objc-missing-property-synthesis-不想对属性进行@synthesize Wno未使用

    Python库 | synthesize-0.0.1-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:synthesize-0.0.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    3D-Synthesize3DviaDepthOrSil.zip

    3D-Synthesize3DviaDepthOrSil.zip,[CVPR 2017]通过建模多视图深度图或轮廓生成和重建三维形状,3D建模使用专门的软件来创建物理对象的数字模型。它是3D计算机图形的一个方面,用于视频游戏,3D打印和VR,以及其他...

    Android代码-Synthesize

    Synthesize Synthesize is an android library which can create layout images in background threads, services, etc without inflating them in activity or fragments. Synthesize Documentation

    Synthesize & Power Analyze

    Synthesize & Power Analyze

    alu.zip_Cyclone_altera_de2_logic_logic synthesize

    Code to synthesize Arithmetic Logic Unit

    Synthesize3DviaDepthOrSil:[CVPR 2017]通过对多视图深度图或轮廓建模来生成和重建3D形状

    Synthesize3DviaDepthOrSil:[CVPR 2017]通过对多视图深度图或轮廓建模来生成和重建3D形状

    synthesize:易于安装的GraphiteD和StatsD

    synthesize中的install脚本旨在使将Graphite和相关服务轻松安装到现代Linux发行版上非常容易。 Synthesize构建为可在Ubuntu 18.04 LTS上运行。 它不会在其他Ubuntu版本或Linux发行版上运行。 该项目的目标不是成为...

Global site tag (gtag.js) - Google Analytics