• 关于iphone开发中的@property和@synthesize的一些见解(转)


    我的体会是@property和@synthesize有以下两个作用:

    1.作用一

    @property是在头文件.h中声明一个变量的setter和getter方法。

    @synthesize是在m文件中定义setter和getter方法的实现。

    2.作用二

    @property,在声明变量方法的时候可以附加定义该变量的属性。如retain,assign,readonly,nonautomic等等。

    但是,有一个点需要解释一下,就是直接使用 变量名赋值 和使用 self.变量 赋值的区别。比如

    .h
    ......

    ObjctName*  nameVarPtr;


    @property(nonautomic,retain) ObjctName*  nameVarPtr;


    .m
    ......

    self.nameVarPtr = [[ObjectName alloc] init];

    int n = [self.nameVarPtr retainCount];                      // n = 2 

    nameVarPtr = [[ObjectName alloc] init];

    n = [nameVarPtr retainCount];                                  // n = 1

    self.nameVarPtr = [[ObjectName alloc] init] 和 nameVarPtr = [[ObjectName alloc] init] 两种赋值方式区别何在呢?

    self.nameVarPtr=xxx 这种赋值方式等价于调用 [self setnameVarPtr:xxx], 而setnameVarPtr:xxx的方法的实现又是依赖于@property的属性的,比如retain,assign等属性。

    nameVarPtr = xxx 的赋值方式,仅仅是对一个指针进行赋值。nameVarPtr仅仅是一个指针变量,记录了xxx的地址。在这个过程中不会调用setter方法,不会调用setter方法,就和@property没有关系,从而,也和retain,assign等属性没有关系。这种赋值方式就是一个简单的指针赋值。

    综上,对成员变量进行赋值,为防内存泄露需要注意的点:

    1.self调用setter方法的方式

    ObjectName*  tmp= [[ObjectName alloc] init];

    self.nameVarPtr =tmp;                 //retainCount=2

    [tmp release];                               //retainCount=1

    2.指针赋值方式,不会调用setter方法

    nameVarPtr= [[ObjectName alloc] init]; // retainCount=1

    @synthesize window=_window之类的写法的理解

    在32位系统中,如果类的 @interface 部分没有进行 ivar 声明,但有 @property 声明,在类的 @implementation 部分有响应的 @synthesize,则会得到类似下面的编译错误:
    Synthesized property 'xX' must either be named the same as a compatible ivar or must explicitly name an ivar
    在 64-bit时,运行时系统会自动给类添加 ivar,添加的 ivar 以一个下划线"_"做前缀。
    上面声明部分的 @synthesize window=_window; 意思是说,window 属性为 _window 实例变量合成访问器方法。也就是说,window属性生成存取方法,这个存取方法就是_window变量的存取方法,它操作的就是_window这个变量.

    下面是一个常见的例子

    @interface MyClass:NSObject{  

    MyObjecct *_myObject;

    }

    @property(nonamtic, retain) MyObjecct *myObject;

    @end
    @implementatin MyClass

    @synthesize myObject=_myObject;

    @end

    这个类中声明了一个变量_myObject,又声明了一个属性叫myObject,

    然后@synthesize生成了属性myObject的存取方法,这个存取方法的名字应该是:setmyObject和getmyObject。

    @synthesize myObject=_myObject的含义就是属性myObject的存取方法是操作_myObject这个变量的。这种用法在Apple的Sample Code中很常见,

    note:.位操作符,在等号左边等于调用setter方法,在等号右边等于调用getter方法。

  • 相关阅读:
    cogs1538 [AHOI2005]LANE 航线规划
    cogs468 [NOI2010]超级钢琴
    [国家集训队2011]数颜色
    动态点分治总结
    点分治总结
    数论知识总结-欧拉函数
    BZOJ2683 简单题
    COGS1871 [国家集训队2011]排队(魏铭)
    LUOGU3278 [SCOI2013]多项式的运算
    BZOJ4491 我也不知道题目名字是什么
  • 原文地址:https://www.cnblogs.com/ganeveryday/p/4417869.html
Copyright © 2020-2023  润新知