iOS中属性 (nonatomic, copy, strong, weak)的使用 By hL
以下内容来自Stackflow的详解
1.Nonatomic
nonatomic is used for multi threading purposes. If we have set the nonatomic attribute at the time of declaration, then any other thread wanting access to that object can access it and give results in respect to multi-threading.
2.Copy
copy is required when the object is mutable. Use this if you need the value of the object as it is at this moment, and you don't want that value to reflect any changes made by other owners of the object. You will need to release the object when you are finished with it because you are retaining the copy.
常用于(block,NSString)
3.Assign
Assign is somewhat the opposite to copy. When calling the getter of an assign property, it returns a reference to the actual data. Typically you use this attribute when you have a property of primitive type (float, int, BOOL...)
常用于简单数据类型(float, int, NSInteger,BOOL,SEL...)
4.Retain
retain is required when the attribute is a pointer to an object. The setter generated by @synthesize will retain (aka add a retain count to) the object. You will need to release the object when you are finished with it. By using retain it will increase the retain count and occupy memory in autorelease pool.
5.Strong
strong is a replacement for the retain attribute, as part of Objective-C Automated Reference Counting (ARC). In non-ARC code it's just a synonym for retain.
Retain与strong是等价的
如 MBProgressHUD.h 中的宏定义
#ifndef MB_STRONG
#if __has_feature(objc_arc)
#define MB_STRONG strong
#else
#define MB_STRONG retain
#endif
#endif
6.Weak
weak is similar to strong except that it won't increase the reference count by 1. It does not become an owner of that object but just holds a reference to it. If the object's reference count drops to 0, even though you may still be pointing to it here, it will be deallocated from memory.
The above link contain both Good information regarding Weak and Strong.
From Stackflow
手思中第三方库AMSmoothAlertView中的相关属性写法参考
关于NSString在MRC时代经常用Copy,Retain属性,而Retain==Strong,所以ARC中很多项目申明为
@property (nonatomic,strong) NSString *userName;
参考第2条的Copy介绍,使用 Copy更安全
@property (nonatomic,Copy) NSString *userName;
相关参考
iOS中属性 (nonatomic, copy, strong, weak)的使用 By hL的更多相关文章
- 对于atomic nonatomic assign retain copy strong weak的简单理解
atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作 1)atomic 设置成员变量的@property属性时,atomic是默认值,提供多线程安全 在多线程环 ...
- iOS中assign、copy 、retain等关键字的含义
iOS中assign.copy .retain等关键字的含义 转自:http://my.oschina.net/majiage/blog/267409 assign: 简单赋值,不更改索引计数cop ...
- iOS知识基础篇--@property,@synthesize, nonatomic,atomic,strong,weak,copy,assign,retain详解
一.@property 这个关键词的唯一作用就是声明getter.setter方法接口. 二.@synthesize 实现setter.getter方法,找不到实例变量则主动创建一个. 三.nonat ...
- iOS中assign,copy,retain之间的区别以及weak和strong的区别(面试)
• copy: 用于希望保持一份传入值的拷贝,而不是值自身的情况,即把原来的对象完整的赋值到另外一地方,重新加载一内存区,一个地方变了不影响另一个地方的对象. • assign: 简单的直接赋值,相 ...
- @property中的copy.strong.weak总结
1.NSString类型的属性为什么用copy NSString类型的属性可以用strong修饰,但会造成一些问题,请看下面代码 #import "ViewController.h" ...
- ios 内存管理与property copy strong weak assign
- (void)fun{ NSString* str = [[NSString alloc] initWithString:@"string"]; NSLog(@"% ...
- iOS中属性Property的常用关键字的使用说明
属性关键字的作用 现在我们iOS开发中,基本都是使用ARC(自动引用计数)技术,来编写我们的代码.因此在属性property中我们经常使用的关键字有strong,weak,assign,copy,no ...
- iOS中assign,copy,retain之间的区别以及weak和strong的区别
@property (nonatomic, assign) NSString *title; 什么是assign,copy,retain之间的区别? assign: 简单赋值,不更改索引计数(Refe ...
- assign,copy,strong,weak,nonatomic的理解
举个例子: NSString *houseOfMM = [[NSString alloc] initWithString:'MM的三室两厅']; 上面一段代码会执行以下两个动作: 1 在堆上分配一段 ...
随机推荐
- VUE的学习_从入门到放弃(一)
一.vue的功能及作用 工作方式如下 1.不用操作DOM 2.单页面应用web项目 简称:SPA 3.当下各种新框架都采用的类似Vue或者类似React的语法去作为主语法,微信小程序/MpVue... ...
- 20道JavaScript经典面试题
该篇文章整理了一些前端经典面试题,附带详解,涉及到JavaScript多方面知识点,满满都是干货-建议收藏阅读 前言 如果这篇文章有帮助到你,️关注+点赞️鼓励一下作者,文章公众号首发,关注 前端南玖 ...
- docker学习:docker容器数据卷
是什么 docker的理念 将运用与运行的环境打包形成容器运行,运行可以伴随着容器,但是我们对数据的要求希望是持久化的 容器之间希望有可能共享数据 docker容器产生的数据,如果不通过docker ...
- 【】VMware vSphere中三种磁盘规格的解释说明
在VMware vSphere中,不管是以前的5.1版本,或者是现在的6.5版本,创建虚拟机时,在创建磁盘时,都会让选择磁盘的置备类型,如下图所示,分为: 厚置备延迟置零 厚置备置零 Thin Pro ...
- PostgreSQL相关知识概念
本文主要介绍PostgreSQL数据库的一些重要知识点, 包括数据库.模式.表空间.用户/角色等概念和关系, 帮助用户理解PostgreSQL数据库的重要概念, 从而能够更好的使用PostgreSQL ...
- pytest 一个测试类怎样使用多种fixture前置方法
fixture()方法写在哪里? @pytest.fixture(scope="范围")写在conftest文件中,如下图 怎么使用fixture()呢?分为一个类中使用一个前置或 ...
- idea 更换 maven ,并更换阿里镜像
1 ctrl + alt + s 打开设置, 找到Maven 修改maven包的地址,然后修改settings.xml 注意了,有时候repository没有,那么需要在settings.xml配置r ...
- C语言 生成一个随机数
随机数的生成 有缺陷的生成方式 生成随机数可以使用 <stdlib.h> 里的 int rand(void); 函数实现! 注释: C语言中还有一个 random() 函数可以获取随机数, ...
- vue3.0+vite项目搭建
npm init vite-app <project-name> cd <project-name> 根据控制台的提示执行: npm install / yarn npm ru ...
- session反序列化
先来了解一下关于session的一些基础知识 什么是session?在计算机中,尤其是在网络应用中,称为"会话控制".Session 对象存储特定用户会话所需的属性及配置信息.这样 ...