当前位置:网站首页>OC中成员变量,实例变量和属性之间的区别和联系
OC中成员变量,实例变量和属性之间的区别和联系
2022-08-02 02:23:00 【~废弃回忆 �༄】
1.成员变量和实例变量
@interface ViewController : UIViewController
{
UIButton*yourButton;
int count;
id data ;
}
@property(nonatomic,strong)UIButton*myButton;
@end
成员变量
在{}中所声明的的变量都是成员变量.所以yourButton,count,data都是成员变量.
实例变量
实例变量本质上就是成员变量,只是实例是针对类而言的,实例是指类的声明.{}中的yourButton就是实例变量.
id是OC特有的类,本质上将id等同于(void*).所以id data属于实例变量
总结:
1.在文件中@interface括号里面的统称为"成员变量",实例变量是成员变量中的一种!
2.实例变量应该是类定义的变量;
3.除去基本数据类型int,float…等,其他类型的变量都叫做实例变量;
4.实例变量+基本数据类型变量=成员变量.
2.属性
属性四种性质
- 原子性:nonatomic,atomic
- 读写权限:readwrite(读写),readonly(只读)
- 内存管理:assign,strong,weak,unsafe_unretioned,copy
- 方法名:getter=,setter=
可选值:nullable,nonnull,null_resettable,null_unspecified.其中nonatomic,atomic,copy在setter/getter中实现.而weak和strong等则是直接作用于成员变量上.
2.1.原子性关键字
默认情况下原子性是atomic,由编译器所合成的方法使用锁机制保证其原子性.如果属性具备nonatomic特性,则不使用同步锁.
2.2.内存管理
1.assign:setter只会执行针对简单变量,例如int,float,NSInteger,CGFloat等;
2.strong:此特性表明该属性定义了一种"拥有关系".为这种属性设置新值,设置方法先保留新值,并释放旧值,然后再将新值赋值上去;
3.weak::此特性表明该属性定义了一种"非拥有关系".为这种属性设置新值时,设置方法既不保留新值,也不释放旧值.此特性同assign类似,然而在属性所指的对象遭到摧毁时,属性值也会清空.
4.unsafe_unretained:此特性同assign相同,但它适用于"对象类型",该特性表达一种"非拥有关系",当目标对象遭到摧毁时,属性不会自动清空,这与weak有区别;
5.copy:此特性表达的所属关系与strong类似.然而设置方法并不保留新值,而是将其拷贝.
边栏推荐
- TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
- The underlying data structure of Redis
- Golang分布式应用之Redis
- yaml
- NAS和私有云盘的区别?1篇文章说清楚
- Analysis of the status quo of digital transformation of manufacturing enterprises
- BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
- Remember a gorm transaction and debug to solve mysql deadlock
- Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
- Safety (2)
猜你喜欢

ros多客户端请求服务

Hash collisions and consistent hashing

oracle查询扫描全表和走索引

面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?

Oracle19c安装图文教程

openGauss切换后state状态显示不对

A good book for newcomers to the workplace
![[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline](/img/08/d693c7e2fff8343b55ff3c1f9317c6.jpg)
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline

Safety (2)

Garbage Collector CMS and G1
随机推荐
Oracle数据类型介绍
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
项目后台技术Express
极大似然估计
2022-08-01 mysql/stoonedb慢SQL-Q18分析
Simple example of libcurl accessing url saved as file
Personal blog system project test
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
MySQL - CRUD operations
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
Handwriting a blogging platform ~ the first day
nacos startup error, the database has been configured, stand-alone startup
永磁同步电机36问(三)——SVPWM代码实现
What to study after the PMP exam?The soft exam ahead is waiting for you~
Safety (1)
Centos7 install postgresql and enable remote access
接口测试神器Apifox究竟有多香?
2022-08-01 Install mysql monitoring tool phhMyAdmin
51. 数字排列