---
title: "ios @property"
author: "董瑞龙"
date: "2011-09-15T11:15:27Z"
tags: []
categories: ["移动开发"]
slug: "ios-property"
draft: false
lastmod: "2011-09-15T11:15:27Z"
id: 1172359
url: "https://rylan.iteye.com/blog/1172359"
---

@property (copy, assign) NSString *title;
什么是assign，copy，retain之间的区别？

assign： 简单赋值，不更改索引计数（Reference Counting）。 
copy： 建立一个索引计数为1的对象，然后释放旧对象 
retain：释放旧的对象，将旧对象的值赋予输入对象，再提高输入对象的索引计数为1 
retain的实际语法为：

- (void)setName:(NSString *)newName {
    if (name != newName) {
       ;
       name = ;
       // name’s retain count has been bumped up by 1
    }
}
说了那么麻烦，其实接下来的话最重要：

?如果你不懂怎么使用他们，那么就这样 ->

使用assign: 对基础数据类型 （NSInteger，CGFloat）和C数据类型（int, float, double, char, 等等） 
使用copy： 对NSString 
使用retain： 对其他NSObject和其子类