当前位置:网站首页>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类似.然而设置方法并不保留新值,而是将其拷贝.
边栏推荐
- 项目后台技术Express
- Power button 1374. Generate each character string is an odd number
- Simple example of libcurl accessing url saved as file
- 周鸿祎称微软抄袭,窃取360安全模式
- NAS和私有云盘的区别?1篇文章说清楚
- GTK RGB图像绘制
- [Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
- Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
- 永磁同步电机36问(三)——SVPWM代码实现
- Use DBeaver for mysql data backup and recovery
猜你喜欢

Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products

Outsourcing worked for three years, it was abolished...

Safety (2)

记一次gorm事务及调试解决mysql死锁

【LeetCode每日一题】——103.二叉树的锯齿形层序遍历

nacos启动报错,已配置数据库,单机启动

AWR分析报告问题求助:SQL如何可以从哪几个方面优化?

项目后台技术Express

Analysis of volatile principle

Ringtone 1161. Maximum In-Layer Elements and
随机推荐
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
790. 数的三次方根
2022河南青训联赛第(三)场
个人博客系统项目测试
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
LeetCode brushing diary: 33. Search and rotate sorted array
2022-08-01 安装mysql监控工具phhMyAdmin
工程师如何对待开源
永磁同步电机36问(二)——机械量与电物理量如何转化?
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
BI - SQL 丨 WHILE
leetcode / anagram in string - some permutation of s1 string is a substring of s2
Outsourcing worked for three years, it was abolished...
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
项目后台技术Express
60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
Handwritten Blog Platform ~ Day Two
极大似然估计
leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串