问题
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:
- There is no ambiguity in what the value of the variable is.
- 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