关于Objective-C 的property,很多iOS开发的新手都会很迷惑,也会包括有经验的iOS开发程序员,

因为Objective-C的property,说多不多,说少却也不少,从MRR(Manual Retain Release )到ARC模式,很多属性功能类似

名称却不一样,比如strony和retain,而且又牵扯到release, autorelease, alloc ,new,copy等一众函数,

所以看着很容被绕进去,迷失在这众多的property,

今天特地梳理支持ARC(iOS 5+) 的propterty的关键词:

strong, weak, copy, readonly/readwrite, atomic/nonatomic,assign,retain

由于strong和retain功能相同,所以会在最后总结的时候,比较所有的这些关键词异同点

1:定义一个property

如下,我们定义一个car 的interface, 该属性有一个property 为 brand(品牌)

// Car.h
#import <Foundation/Foundation.h> @interface Car : NSObject @property NSString *brand; @end
//  Car.m

#import "Car.h"

@implementation car

@synthesize brand = _brand;

@end

2:属性的getter 和setter方法和synthesize

编译器会默认的为属性生成getter 和setter方法

- (NSString *)brand
{
return _brand;//_brand即使synthesize指定的属性名称
} - (void)setBrand:(NSString *)brand
{
_brand = brand;
}

注意:synthesize 不是必须明确声明,除非手动些属性的getter和setter方法

否则,complier默认生成的code 如下格式

@synthesize brand = _brand

synthesize关键字缺省方式是:将brand属性对应的实例便令声明为_brand,即_ + propertyname

当然可以自己指定比如brand = xxxbrand

编译器默认会将getter方法和属性同名,setter方法为set+属性(首字母大写),例如setBrand

可以指定属性的getter和setter方法名称

@property (nonatomic,getter=getBrand, setter = setBrand:)NSString *brand;

3:readonly和readwrite

声明 readonly属性

@property (readonly,getter=getBrand)NSString *brand;

声明了readonly的属性,一定没有setter方法,所以你可以使用getter或者 dot方法 获取属性的值,但肯定不可以使用

setter方法修改

Car *c = [[Car alloc]init];0

[c setBrand:@"tony"]; //Error

NSLog(@"%@", [c getBrand]);

声明为readonly 不意味着我们不可以修改,propery 的accessor method 本来就是提供给其他接口调用的,

在我们内部,是直接操纵_brand实例的, 如下

// Car.h
- (void)setBrand:(NSString *)brand;
// Car.m
- (void )setBrand:(NSString *)brand;
{
_brand = brand;
}

readonly 对应的readwrite 属性, readwrite 是default的behavior,可以不显示声明

@property (readwrite)NSString *brand;

4:atomic, nonatomic

atomic 原子的,default behavior,支持多线程操作, 代价是开销比nonatomic大

如果声明一个属性,确定不会运行在多线程的环境,可以直接声明为nonatomic

@property (nonatomic)NSString *brand;

内存管理

Ojective-C 内存模型中,不管是MRR,还是ARC,都是根据对象的引用计数来确定对象是否回收

当对象的 引用计数为0,就会被回收(不一定是立即回收)

从引用计数的原理出发,Objective-C 提出了三种相关的是属性

strong, weak, copy

1:strony 属性

strong属性,即声明一个对象拥有另外一个对象,引用计数 + 1

加入 A对象 有个strong 属性的实例对象B, 则在A对象释放之前,B是不会释放的

看如下sample code

声明一个类Person,Person有一个属性name

//
// Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic) NSString *name; @end
//  Person.m
#import "Person.h" @implementation Person - (NSString *)description
{
return self.name;
}
@end

声明一个Car 类,有两个属性 model和driver

//  Car.h

#import <Foundation/Foundation.h>
#import "Person.h" @interface Car : NSObject @property (nonatomic) NSString *model;
@property (nonatomic,strong) Person *driver; @end

main.m

// main.m
#import <Foundation/Foundation.h>
#import "Car.h"
#import "Person.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *john = [[Person alloc] init];
john.name = @"John"; Car *honda = [[Car alloc] init];
honda.model = @"Honda Civic";
honda.driver = john; NSLog(@"%@ is driving the %@", honda.driver, honda.model);
}
return ;
}

可以看到,hoda对象对driver对象负责

2:weak属性

strong 属性,很直观的显示对象直接的关系,但也很容易引起内存泄露,

看如下代码,我们稍微小改了Person.h ,增加了Car 属性,

// Person.h
#import <Foundation/Foundation.h> @class Car; @interface Person : NSObject @property (nonatomic) NSString *name;
@property (nonatomic, strong) Car *car; @end

则在main.m

增加一行

john.car = honda;

则此时,john和hoda互相持有对方

二者这件形成了 retain cycle(互相持有的循环),就是典型的内存泄露的一种情况

如果car声明为weak,这个问题即迎刃而解

@property (nonatomic, weak) Car *car;

3:copy 属性

copy不同于strong, 并不拥有copy的对象,而是复制该对象(必须conform NSCopying protocol )

sample code

#import <Foundation/Foundation.h>
#import "Car.h"
#import "Person.h" int main(int argc, char *argv[])
{ Car *bmw = [[Car alloc]init]; Person *driver = [[Person alloc]init]; NSMutableString *name = [NSMutableString stringWithFormat:@"zhangsan"];//mutable 的name 为zhangsan [driver setName:name];
[name setString:@"lisi"];//修改为lisi
[driver setCar:bmw]; [bmw setModel:@"X5"];
[bmw setDriver:driver]; NSLog(@"%@", [driver name]);//仍然是zhangsan }

copy 属性在被赋值的时候就copy了该值,所以即使该值是mutable的,属性的值也不会被修改,

现对于strong的持有对象,copy特别适合只是简单存储值得属性

以上都是ARC支持的属性。

4:属性retain  

 等同于 strong

5:unsafe_unretained

类似于weak,和weak不同的是,如果weak 属性指向的对象如果被销毁后,weak 属性被置为nil,

而 unsafe_unretained 不会,则又可能形成悬垂指针的问题

6:assign

默认的属性,不需要显示声明

指定setter方法进行简单的赋值 ,对基础数据类型(NSInteger,CGFloat)

和C数据类型(int, float, double, char)等等。

 总结:

atomic //default

nonatomic

strong=retain //default

weak

assign //default

unsafe_unretained

copy

readonly

readwrite //default

引用:

http://rypress.com/tutorials/objective-c/properties

http://rypress.com/tutorials/objective-c/memory-management

http://stackoverflow.com/questions/8927727/objective-c-arc-strong-vs-retain-and-weak-vs-assign

Objective-C Properties 详解的更多相关文章

  1. log4j.properties 详解与配置步骤(转)

    找的文章,供参考使用 转自 log4j.properties 详解与配置步骤 一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR ...

  2. SpringBoot配置文件 application.properties详解

    SpringBoot配置文件 application.properties详解   本文转载:https://www.cnblogs.com/louby/p/8565027.html 阅读过程中若发现 ...

  3. log4j.properties 详解与配置步骤

    一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR 为严重错误 主要是程序的错误WARN 为一般警告,比如session丢失IN ...

  4. [转]application.properties详解 --springBoot配置文件

    本文转载:http://blog.csdn.net/lpfsuperman/article/details/78287265###; # spring boot application.propert ...

  5. application.properties详解 --springBoot配置文件【转载】

    # spring boot application.properties配置的各个属性详解 # 该示例文件作为标准提供.(官方文档 翻译过来的) # 还是花了些功夫翻译,各位如果转发,请留下本文地址, ...

  6. application.properties详解 --springBoot配置文件

    本文转载:http://blog.csdn.net/lpfsuperman/article/details/78287265###; # spring boot application.propert ...

  7. 转--log4j.properties 详解与配置步骤

    一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR 为严重错误 主要是程序的错误WARN 为一般警告,比如session丢失IN ...

  8. log4j.properties 详解与配置步骤总结

    先提供一个项目中使用log4j.properties配置 #log4j.rootLogger=WARN, stdout, file log4j.rootLogger=INFO,console,dail ...

  9. 【转】log4j.properties 详解与配置步骤 - edward0830ly的专栏 - 博客频道 - CSDN.NET

    一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR 为严重错误 主要是程序的错误WARN 为一般警告,比如session丢失IN ...

  10. (转)spring hibernate properties详解

    转载地址:http://blog.sina.com.cn/s/blog_692d0a650100xyqx.html Hibernate配置属性 hibernate.dialect:一个Hibernat ...

随机推荐

  1. Scala函数高级操作

    字符串高级操作:***** 非常重要 将函数赋值给变量/值def sayHello(name:String): Unit = { println(s"Hello:$name")} ...

  2. 线上服务器CPU100%排查

    某服务器上部署了若干tomcat实例,即若干垂直切分的Java站点服务,以及若干Java微服务,突然收到运维的CPU异常告警. 问:如何定位是哪个服务进程导致CPU过载,哪个线程导致CPU过载,哪段代 ...

  3. CentOS vim的使用

    安装vim工具 [root@bogon ~]# yum install -y vim-enhanced 卸载vim工具 [root@bogon ~]# yum remove -y vim* vim常用 ...

  4. python获取ip地址

    #!/usr/bin/env python # -*- coding: utf-8 -*- import os import socket,fcntl,struct #crontab下shell命令无 ...

  5. [Codeforces712D] Memory and Scores(DP+前缀和优化)(不用单调队列)

    [Codeforces712D] Memory and Scores(DP+前缀和优化)(不用单调队列) 题面 两个人玩游戏,共进行t轮,每人每轮从[-k,k]中选出一个数字,将其加到自己的总分中.已 ...

  6. BZOJ 3262(Treap+树状数组)

    题面 传送门 分析 分三维考虑 对第一维,直接排序 对第二维和第三维,我们这样考虑 朴素的方法是建k棵Treap,第i棵Treap里存第二维值为k的第三维数值 每次查询一组(a,b,c),只要在1~b ...

  7. tomcat内存使用情况

    预发布阿里云服务器的容器 tomcat会自己无缘无故重启,故引出一些查看tomcat内存使用情况观察的细枝末节: 1️⃣当前端口号进程信息和GC使用情况(1)显示端口的PID:lsof -i:端口示例 ...

  8. 获取class的儿子,报错undefined

       var tds = document.getElementsByClassName("dv1")[0].children     console.log(tds) 因为cla ...

  9. CSS 针对谷歌浏览器(Chrome) safari的webkit核心浏览器CSS hack

    @media screen and (-webkit-min-device-pixel-ratio:0) { ul#navUL ul a{padding:8px 2px;word-break:keep ...

  10. c++求中位数

    #include <iostream> #include <cassert> #include <stack> #include <math.h> us ...