1. 创建属性

手动定义属性:

声明一个成员变量,然后写属性的 getter/setter 声明及实现的方法。

xcode 帮忙定义属性:

xcode4.4之前 :

  • @property = setter 方法声明 + getter 方法声明
  • @synthesize = ivar + setter 实现 + getter 实现(setter 方法要不要实现,根据 readonly)

【注意】 @synthesize property = property;//如果不存在 property,则会创建一个 property 成员变量,如果存在,则不会添加成员变量。(property 其实可以也指定到一个已经存在的私有成员变量上,但是不推荐使用)

后来xcode编译器有了自动合成机制,其实 @property 和 @synthesize 的职责没变,只是编译器会默认添加@synthesize , 看起来是@property 直接可以直接生成带下划线的属性及getter/setter 方法的声明和实现!实际上还是各司其职。

什么情况下自动合成失效?

  • 分类、协议中的@property
  • 使用了@dynamic 修饰。@dynamic 是告诉编译器 不用自动生成 getter/setter 方法,用户会在运行时动态添加。从而避免编译时出现警告。
  • 当使用readwrite 时,开发者将 getter/setter 方法均手动实现
  • 当使用readonly 时,开发者将 getter 方法手动实现


    2. @property的四种修饰词

  1. get/set 方法的线程管理

    • atomic: 原子性(默认),对属性的 get/set 方法加原子锁。需要注意的是只对指针加了锁, 也就是只有对属性的 指针的 set 方法的操作是线程安全的;如果对象内部进行数组的添加和移除元素、字符串的拼接等操作是非线程安全的,。
    • nonatomic: 非原子性。只在主线程中使用时,涉及不到线程安全问题,建议使用, 因为性能高。
  2. get/set 方法的命名

    • setter: 设置set 方法的名字,注意需要加冒号(默认是set + 属性名首字母大写 + 冒号,eg: setName: )
    • getter: 设置get方法的名字(默认是属性名)
  1. 是否需要生成 set 方法

    • readwrite: 同时生成 get/set 方法(默认)
    • readonly: 只生成 get 方法
  2. 设置 set 方法的内存管理方式

  • MRC:
    • retain: 强引用,release 旧值, retain 新值(只用于OC对象)
    • assign: 对对象来说弱引用。直接赋值,不做任何内存管理,对象释放后,访问指针会崩溃。是默认的修饰词,可用于基本数据类型和对象,一般用于基本数据类型
    • copy/mutableCopy: 强引用,release 旧值, copy 新值(只用于OC对象, 一般用于 NSString 和 Block)
  • ARC:

    • strong:代替retain
    • weak: 代替 assign 修饰对象那部分, 对象被释放后,指针会被自动置为nil
    • assign: 对对象来说弱引用。直接赋值,不做任何内存管理,对象释放后,访问指针会崩溃。是默认的修饰词,可用于基本数据类型和对象,一般用于基本数据类型
    • copy/mutableCopy: 强引用,release 旧值, copy 新值(只用于OC对象, 一般用于 NSString 和 Block)

      3. objc_setProperty 源码探索

      objc_setProperty —-> reallySetProperty 源码:

      1. static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy)
      2. {
      3. if (offset == 0) {
      4. object_setClass(self, newValue);
      5. return;
      6. }
      7. id oldValue;
      8. id *slot = (id*) ((char*)self + offset);
      9. if (copy) {
      10. newValue = [newValue copyWithZone:nil];
      11. } else if (mutableCopy) {
      12. newValue = [newValue mutableCopyWithZone:nil];
      13. } else {
      14. if (*slot == newValue) return;
      15. newValue = objc_retain(newValue);
      16. }
      17. if (!atomic) {
      18. oldValue = *slot;
      19. *slot = newValue;
      20. } else {
      21. spinlock_t& slotlock = PropertyLocks[slot];
      22. slotlock.lock();
      23. oldValue = *slot;
      24. *slot = newValue;
      25. slotlock.unlock();
      26. }
      27. objc_release(oldValue);
      28. }

objc_getProperty

  1. id objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic) {
  2. if (offset == 0) {
  3. return object_getClass(self);
  4. }
  5. // Retain release world
  6. id *slot = (id*) ((char*)self + offset);
  7. if (!atomic) return *slot;
  8. // Atomic retain release world
  9. spinlock_t& slotlock = PropertyLocks[slot];
  10. slotlock.lock();
  11. id value = objc_retain(*slot);
  12. slotlock.unlock();
  13. // for performance, we (safely) issue the autorelease OUTSIDE of the spinlock.
  14. return objc_autoreleaseReturnValue(value);
  15. }