当前位置:网站首页>使用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方式.此方法既能提高读取速度,又能保证相关属性的"内存管理".
边栏推荐
- 考完PMP学什么?前方软考等着你~
- Service discovery of kubernetes
- The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
- 机器人领域期刊会议汇总
- "NetEase Internship" Weekly Diary (3)
- Unable to log in to the Westward Journey
- 2022-07-30 mysql8执行慢SQL-Q17分析
- Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
- 2022河南青训联赛第(三)场
- 【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
猜你喜欢
随机推荐
【web】理解 Cookie 和 Session 机制
项目后台技术Express
Simple example of libcurl accessing url saved as file
Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
Unable to log in to the Westward Journey
Win Go development kit installation configuration, GoLand configuration
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
messy website
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
29. 删除链表中重复的节点
swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
Centos7 install postgresql and enable remote access
Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
[LeetCode Daily Question]——654. The largest binary tree
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
ALCCIKERS Shane 20191114
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
网络层解析——IP协议、地址管理、路由选择
BI-SQL丨WHILE









