当前位置:网站首页>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类似.然而设置方法并不保留新值,而是将其拷贝.
边栏推荐
- 2022河南青训联赛第(三)场
- Golang分布式应用之定时任务
- GTK RGB图像绘制
- The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
- 【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
- 53. 最小的k个数
- LeetCode brushing diary: 33. Search and rotate sorted array
- libcurl访问url保存为文件的简单示例
- Golang分布式应用之Redis
- MySQL8 download, start, configure, verify
猜你喜欢
2023年起,这些地区软考成绩低于45分也能拿证
【web】理解 Cookie 和 Session 机制
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
[LeetCode Daily Question]——654. The largest binary tree
oracle查询扫描全表和走索引
2022 Henan Youth Training League Game (3)
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
Speed up your programs with bitwise operations
FOFAHUB usage test
51. 数字排列
随机推荐
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
AI target segmentation capability for fast video cutout without green screen
Garbage Collector CMS and G1
Remember a pit for gorm initialization
永磁同步电机36问(三)——SVPWM代码实现
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
51. 数字排列
messy website
[ORB_SLAM2] SetPose, UpdatePoseMatrices
The state status is displayed incorrectly after the openGauss switch
mysql 查看死锁
四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
Handwriting a blogging platform ~ Day 3
"NetEase Internship" Weekly Diary (3)
欧拉公式的证明
openGauss切换后state状态显示不对
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
Redis 底层的数据结构
EFCore 反向工程