Is there automatic initialization for attributes?

半世苍凉 提交于 2019-12-12 23:45:35

问题


I was wondering if attributes were automatically set to nil during an object's initialization or they have random values?


回答1:


If this question is indicative of Objective-C, I'd say that you should initialize all values of a variable when you declare them.

Explicitly initializing variables when they're declared gives you two benefits:

  1. There is no ambiguity in what the value of the variable is.
  2. Readability for others who read your code.



回答2:


All instance variables are guaranteed to be initialized to nil or zero. This goes for non-object iVars as well, (int, BOOL, float).




回答3:


In Java, the data members of a class are set their default values. I.e string = null ,int =0.



来源:https://stackoverflow.com/questions/4528098/is-there-automatic-initialization-for-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!