iOS编程过程中,经常看到一些属性前面有些修饰符,比如copy,retain等。

这些关键字,是Object-C语言中,对于Property的setter。

Mac官网:

The Objective-C Programming Language – Declared Properties – Setter Semantics

中的解释是:

Setter Semantics

These attributes specify the semantics of a set accessor. They are mutually exclusive.

strong

Specifies that there is a strong (owning) relationship to the destination object.

weak

Specifies that there is a weak (non-owning) relationship to the destination object.

If the destination object is deallocated, the property value is automatically set to nil.

(Weak properties are not supported on OS X v10.6 and iOS 4; use assigninstead.)

copy

Specifies that a copy of the object should be used for assignment.

The previous value is sent a release message.

The copy is made by invoking the copy method. This attribute is valid only for object types, which must implement the NSCopying protocol.

assign

Specifies that the setter uses simple assignment. This attribute is the default.

You use this attribute for scalar types such as NSInteger and CGRect.

retain

Specifies that retain should be invoked on the object upon assignment.

The previous value is sent a release message.

In OS X v10.6 and later, you can use the __attribute__ keyword to specify that a Core Foundation property should be treated like an Objective-C object for memory management:

@property(retain) __attribute__((NSObject)) CFDictionaryRef myDictionary;

即使看完解释,其实也很难理解具体的含义。

后来是看了很多人的总结,,再经过一些实际编程的折腾,尤其是:

【已解决】iOS程序出现警告:ARC Semantic Issue,Assigning retained object to unsafe property;object will be released after assignment,之后程序运行出错:Thread 1: EXC_BAD_ACCESS(code=1,address=0xe0000010)

才稍微有所理解的。

整理如下:

想要理解这几个setter的含义,必须先对Object-C中的ARC有所了解。

关于ARC,简单说几句就是,编译器自动帮你实现了引用技术,不用再麻烦你去写retain,release等词去操心引用技术的事情了。

关于ARC更详细的解释,可参考:

【总结】iOS的自动引用计数(ARC,Automatic Reference Counting)

strong和weak

自从有了ARC,就可以使用weak或strong来说明属性是弱引用还是强引用;

assign,retain和copy

没有ARC之前,都是使用assign,retain,copy来修饰属性的。

assign,主要用于数值类变量,即标量,直接赋值即可,不涉及引用计数的变化(标量值,也没有引用技术可以供管理);

copy,是拷贝一份新的对象,引用计数重置为1,释放旧的对象;

retain,是对于原对象,引用计数加1,不会释放旧的对象;

【整理】Object-C中的属性(Property)的Setter:assign,copy,retain,weak,strong之间的区别和联系的更多相关文章

  1. Objective-C 在Categroy中创建属性(Property)

    Objective-c中category是不能直接创建属性的,这时候我们要用到Objc的runtime来实现 用到的方法有两个 一个是get方法 一个set方法 //get方法objc_getAsso ...

  2. C#中的属性-Property

    C#的属性一直都有用,但具体了解的不是很深,而且一些注意事项也没有太在意过,糊里糊涂的用着.这两天看了C#的书专门学习了一下属性,这才知道,原来属性也有这么多东西~ ~今天记录一下,算是对学习的一个检 ...

  3. property中的strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解

    iOS5中加入了新知识,就是ARC,其实我并不是很喜欢它,因为习惯了自己管理内存.但是学习还是很有必要的. 在iOS开发过程中,属性的定义往往与retain, assign, copy有关,我想大家都 ...

  4. iOS property中的strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解

    iOS5中加入了新知识,就是ARC,其实我并不是很喜欢它,因为习惯了自己管理内存.但是学习还是很有必要的. 在iOS开发过程中,属性的定义往往与retain, assign, copy有关,我想大家都 ...

  5. Python: 浅淡Python中的属性(property)

    起源:项目过程中需要研究youtube_dl这个开源组件,翻阅其中对类的使用,对比c#及Delphi中实现,感觉Python属性机制挺有意思.区别与高级编程语言之单一入口,在类之属性这一方面,它随意的 ...

  6. 从Object对象中读取属性的值

    C#是强类型语言,强到多变态?一个对象没有定义某个属性,你想点出来,IDE直接给你报语法错误.远不如js那么自由,想怎么点怎么点. 如果你从别人接口中拿到的就是Object类型,你想获取某个属性的值怎 ...

  7. WPF样式中TargetType 属性 (Property) 和 x:Key 属性 (Attribute)

    如第一个示例所示,如果将 TargetType 属性设置为 TextBlock 而不为样式分配 x:Key,样式就会应用于所有 TextBlock 元素.这种情况下,x:Key 隐式设置为 {x:Ty ...

  8. C# 从Object对象中读取属性的值

    https://www.cnblogs.com/xbblogs/p/7739483.html

  9. 在c++中,标准输入string时cin 与getline两个函数之间的区别

    cin: cin函数是标准库的输入函数,在读取string时遵循以下规则: 1)读取并忽略开头所有的空白符(包括空格.换行符.制表符). 2)读取字符直到遇到空白符,读取终止. 例如: 当输入的是“  ...

随机推荐

  1. android 设置头像以及裁剪功能

    在android的开发过程中,经常遇到设置用户头像以及裁剪图像大小的功能.昨天我遇到了设置用户头像的功能,开始不知道怎么搞,在技术群里问也没人回 答,就研究了微信用户设置头像的功能,了解到用户设置图像 ...

  2. Unix/Linux环境C编程入门教程(5) Red Hat Enterprise Linux(RHEL)环境搭建

    Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 通过./a.out ./Y.out执行出结果,证明C++程序编译成功,也就说明li ...

  3. [poj 2978]Colored Stones[状态压缩DP]

    题意: 给出n个石子,一共m种颜色.问最少去掉几个石子使得同种颜色全连续. 思路见注释. #include <algorithm> #include <cstdio> #inc ...

  4. 转载 LayoutInflater的inflate函数用法详解

    http://www.open-open.com/lib/view/open1328837587484.html LayoutInflater的inflate函数用法详解 LayoutInflater ...

  5. HOJ1014

    Niven Numbers My Tags   (Edit)   Source : Unknown   Time limit : 1 sec   Memory limit : 32 M Submitt ...

  6. leetcode Reverse Integer python

    class Solution(object): def reverse(self, x): """ :type x: int :rtype: int "&quo ...

  7. smarty函数-转载

    Smarty常用函数 2009-08-13 14:05:55|  分类: Php |举报 |字号 订阅   1 .include_once语句: 引用文件路径,路径必需正确.   eg:include ...

  8. 不包含任何UserControl

    奇怪了,以前做控件的时候都没有这个问题,哎,又堕落了1年,什么都忘了 创建自定义控件 可以继承现有控件,也可以继承userContrl类,但是现在有个问题 很多网上的资料说,啊,直接创建一个自定义控件 ...

  9. test md

    [TOC] Glossary SUT SYSTEM UNDER TEST CUT CLASS UNDER TEST MUT METHOD UNDER TEST Tests without Use of ...

  10. SQL Server 数据岸问题

    create table t2(x int constraint pk_t2 primary key);go insert into t2(x) values(1),(2),(3),(5),(7),( ...