当前位置:网站首页>使用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方式.此方法既能提高读取速度,又能保证相关属性的"内存管理".
边栏推荐
- Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
- libcurl访问url保存为文件的简单示例
- Install mysql using docker
- swift项目,sqlcipher3 -> 4,无法打开旧版数据库有办法解决吗
- Safety (1)
- 网络层解析——IP协议、地址管理、路由选择
- 局部敏感哈希:如何在常数时间内搜索Embedding最近邻
- 【LeetCode Daily Question】——704. Binary Search
- BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
- [Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
猜你喜欢
使用DBeaver进行mysql数据备份与恢复
"NetEase Internship" Weekly Diary (3)
Handwriting a blogging platform ~ Day 3
Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
列表常用方法
CASE2023
Remember a pit for gorm initialization
Chopper webshell feature analysis
2022-08-01 mysql/stoonedb慢SQL-Q18分析
随机推荐
列表常用方法
JVM调优实战
29. 删除链表中重复的节点
leetcode / anagram in string - some permutation of s1 string is a substring of s2
【LeetCode每日一题】——704.二分查找
Redis Persistence - RDB and AOF
FOFAHUB使用测试
Simple example of libcurl accessing url saved as file
优炫数据库导库导错了能恢复吗?
60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
51. 数字排列
What to study after the PMP exam?The soft exam ahead is waiting for you~
oracle查询扫描全表和走索引
20. 用两个栈实现队列
BI - SQL 丨 WHILE
Service discovery of kubernetes
IMU预积分的简单理解
【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
记一次gorm事务及调试解决mysql死锁