What is better and why ?
What is better in such situations as the class init method and usual variables in a code ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Explicit typing information is always better unless you just can’t use it for some reason (see below).
It allows the compiler to much more stringently validate the code and will catch many errors at compile time that would otherwise cause your app to crash at runtime.
A long, long, time ago, everything in the APIs used
id. This proved to be a complete pain in the butt; fragile and led to many crashes that would have been caught with specific types.So, it was changed. (This was ~1994).
For
init, you have no choice but to use the generic(id)return type. Objective-C does not support either co-variant or contra-variant declarations, nor is there a mechanism for generalizing the declaration ofinitwhile also providing support for specific type checking.Same goes for
retain,objectAtIndex:,addObject:and many other methods that take or return one of many kinds of objects (or take ’em as arguments).And, no, there is absolutely no performance difference whatsoever between
idand, say,NSView*.If you wrote:
And in a subclass:
You’d get compiler warnings out the wazoo most likely or you’d have to typecast out the wazoo.