当前位置:网站首页>使用self和_(下划线)的区别
使用self和_(下划线)的区别
2022-08-02 02:23:00 【~废弃回忆 �༄】
self.name和_named的区别,本质,那个会更好
本质
self.name是对属性的访问,是调用的name属性的getter/setter方法;
_name是对局部变量的访问,等价于self->name,并不会调用getter/setter方法;
使用那种方法会更好
推荐使用_xx的理由
1.在对象内部尽量直接访问实例变量;
2.不通过OC方法派发,速度更快;
推荐使用self.xx的理由
1.他可以兼容懒加载;
2.避免了使用下划线的时候忽视了self这个指针,例如self->_xx;
3._xx更容易造成循环引用;
4.使用_xx是获取不到父类的属性,因为他只是对局部变量的访问;
5.self.xx使用存在内存管理等优势,更加安全可靠.
总结
在写入实例变量的还是,使用self.xx 方式,通过其"设置方法"来设置;而在读取实例变量的时候,则使用_xx方式.此方法既能提高读取速度,又能保证相关属性的"内存管理".
边栏推荐
猜你喜欢

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

【web】Understanding Cookie and Session Mechanism

【web】理解 Cookie 和 Session 机制

Service discovery of kubernetes

菜刀webshell特征分析

2022-08-01 mysql/stoonedb慢SQL-Q18分析

Ringtone 1161. Maximum In-Layer Elements and

nacos startup error, the database has been configured, stand-alone startup

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

记一个gorm初始化的坑
随机推荐
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
2022-08-01 mysql/stoonedb慢SQL-Q18分析
BI - SQL 丨 WHILE
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
Handwritten Blog Platform ~ Day Two
789. 数的范围
LeetCode Review Diary: 153. Find the Minimum Value in a Rotated Sort Array
ofstream,ifstream,fstream read and write files
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
Electronic Manufacturing Warehouse Barcode Management System Solution
NIO‘s Sword(牛客多校赛)
MySQL - CRUD operations
NIO's Sword
GTK RGB图像绘制
Speed up your programs with bitwise operations
记一个gorm初始化的坑
数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
2023年起,这些地区软考成绩低于45分也能拿证
AWR analysis report questions for help: How can SQL be optimized from what aspects?