当前位置:网站首页>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-07-30 mysql8执行慢SQL-Q17分析
- FOFAHUB使用测试
- Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
- Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
- How engineers treat open source
- Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
- Garbage Collector CMS and G1
- Golang分布式应用之Redis
- 周鸿祎称微软抄袭,窃取360安全模式
- 789. 数的范围
猜你喜欢
![[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games](/img/8a/07ca69c6dcc22757156cb615e241f8.png)
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games

The underlying data structure of Redis

Electronic Manufacturing Warehouse Barcode Management System Solution

LeetCode brush diary: LCP 03. Machine's adventure

用位运算为你的程序加速
![[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)](/img/ed/ffced88c9d23c20ccf380494051381.jpg)
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)

Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案

2022河南青训联赛第(三)场

51. 数字排列

AI target segmentation capability for fast video cutout without green screen
随机推荐
Lombok
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
cocos中使用async await异步加载资源
Handwritten Blog Platform ~ Day Two
2022-08-01 Reflection
LeetCode 213. Robbery II (2022.08.01)
Redis for distributed applications in Golang
How engineers treat open source
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
A good book for newcomers to the workplace
拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货
2022-07-30 mysql8 executes slow SQL-Q17 analysis
2022-07-30 mysql8执行慢SQL-Q17分析
ofstream,ifstream,fstream read and write files
Chopper webshell feature analysis
AOF rewrite
Use DBeaver for mysql data backup and recovery
MySQL - CRUD operations