ios – 为什么我应该直接从初始化方法中访问实例变量?

ios – 为什么我应该直接从初始化方法中访问实例变量?,第1张

概述Apple Programming with Objective-C文档指出: You should always access the instance variables directly from within an initialization method because at the time a property is set, the rest of the object may Apple Programming with Objective-C文档指出:

You should always access the instance variables directly from within
an initialization method because at the time a property is set,the
rest of the object may not yet be completely initialized. Even if you
don’t provIDe custom accessor methods or kNow of any sIDe effects from
within your own class,a future subclass may very well overrIDe the
behavior.

但是我不知道setter方法会有什么副作用,请举个例子来解释为什么我必须直接从初始化方法中访问实例变量

解决方法 答案很简单 – 它是代码味道.像self.foobar这样的点符号= Objective-C中的东西只是用于消息传递的语法糖. 向自己发送消息通常很好.但是有两种情况需要避免它们: 1.创建对象时,和 2.当物体被摧毁时. 在这两次,对象处于一种奇怪的中间状态.它缺乏诚信.在这些时间调用方法是代码味道,因为每个方法都应该在对象上 *** 作时保持不变量. 总结

以上是内存溢出为你收集整理的ios – 为什么我应该直接从初始化方法中访问实例变量?全部内容,希望文章能够帮你解决ios – 为什么我应该直接从初始化方法中访问实例变量?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1074494.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存