Reputation: 1451
I have an NSObject class, which has some primitive properties:
@interface MyClass: NSObject
@property (nonatomic, assign) BOOL boolProp;
@property (nonatomic, assign) NSInteger intProp;
@end
@implementation MyClass
// No init implemented
@end
From what I experienced, when there is no init method, then the properties default to "reasonable" values (BOOL
to NO
, intProp
to 0
). Is this defined behaviour? If yes, is there any Apple documentation for this behaviour?
Upvotes: 0
Views: 952
Reputation: 21808
As for Apple documentation check Object Initialization. It mentions the topic raised in your question. For example
The default set-to-zero initialization performed on an instance variable during allocation is often sufficient
Upvotes: 5
Reputation: 16774
By default the structure behind the implementation (a C structure) is being set to 0 as a whole. Imagine using memset(self, 0, sizeof(_type))
. All the rest is just typecasting. That means all pointers are Null
, all integers or other numbers are 0
, all boolean values are 0
, NO
or false
.
I am not sure there is a documentation about it but if it is then it is on the level of allocation of NSObject
. These defaults are there in general the way are described it but that might not actually meant they are guarantied. What I mean by that is that although this functionality of setting all to zero is working there is still a chance that this behaviour may not be true for some old version or for some future version. (unless this behaviour is in fact documented).
It should also be noted that as said the memory of structure itself is set to zero so no setters are being called to put things to zero and no logic is executed no matter how you setup your class.
Upvotes: 4