On May 23, 2012, at 12:15 PM, Jason Teagle wrote: > Fair point, and well noted. I think it's a shame that objects would be so > cavalier about their existing state (isn't that just sloppy? Makes it far too > easy to leak memory)
No, it's part of the contract of object initialization. Initializer methods may only be called on 'blank' objects returned from +alloc. They're like constructors in C++/Java, just not built into the language*. If you want a method to _reset_ an existing object, that's something different. Some classes have this, like -[NSMutableArray removeAllObjects]. It fundamentally doesn't make sense for immutable objects like NSString, however, since it could change their contents. I guess you could say it's 'easy' to leak memory by calling -init more than once, but only in the sense that it's 'easy' to leak by calling -retain too many times (without ARC), or to crash by writing past the end of a data's -mutableBytes. Objective-C isn't a totally safe language like Java or Ruby. :) —Jens * Keep in mind that Objective-C has been since the early '80s and was primarily influenced by Smalltalk-80; the alloc/init dichotomy comes directly from Smalltalk. C++ existed at the time but hadn't become mainstream yet. _______________________________________________ Cocoa-dev mailing list ([email protected]) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
