Re: Detecting Initialization
On Wed, Jan 28, 2009 at 8:48 PM, David H. Silber wrote: > On Thu, Jan 29, 2009 at 11:39:21AM +1100, Graham Cox wrote: >> >> On 29 Jan 2009, at 11:06 am, David H. Silber wrote: >> >> >Is there some means of detecting if an instance has been initialized? >> > >> >I'm thinking not. Apple's "NSObject Class Reference" says "The init >> >method defined in the NSObject class does no initialization", and >> >googling around on the topic has not produced any means of doing >> >so. I >> >ask here in case someone has some knowledge I have not been able to >> >find. Even a definitive "No" would help, as I could stop searching >> >for >> >an answer which doesn't exist. > >> >> The answer is no, but in order to comply with Cocoa's non-optional >> rule on this, if an instance exists, it's initialised. Otherwise >> you've made a grave error. >> >> WAYTTD? > > I was hoping to be able to write a unit test which would confirm that an > object had been properly initialized. What counts as "initialized" depends entirely on the class in question. NSObject does nothing in its initializer. As a consequence, an NSObject instance is fully initialized as soon as it comes back from +alloc, and stays that way until it is destroyed. There's no way to have an uninitialized NSObject instance. For your classes, you get to decide what counts as initialized. Do you want to check for certain necessary bits of state? Do you want to just set an "initialized" flag that you check? It's all up to you. Keep in mind that an allocated but uninitialized object has its instance variables filled with zero, that will help you to identify that state. Mike ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Detecting Initialization
On Jan 28, 2009, at 4:46 PM, cocoa-dev-requ...@lists.apple.com wrote: On Thu, Jan 29, 2009 at 11:39:21AM +1100, Graham Cox wrote: On 29 Jan 2009, at 11:06 am, David H. Silber wrote: Is there some means of detecting if an instance has been initialized? I'm thinking not. Apple's "NSObject Class Reference" says "The init method defined in the NSObject class does no initialization", and googling around on the topic has not produced any means of doing so. I ask here in case someone has some knowledge I have not been able to find. Even a definitive "No" would help, as I could stop searching for an answer which doesn't exist. The answer is no, but in order to comply with Cocoa's non-optional rule on this, if an instance exists, it's initialised. Otherwise you've made a grave error. WAYTTD? I was hoping to be able to write a unit test which would confirm that an object had been properly initialized. I have written unit tests that confirm/validate object initialization. The ones I have written use accessors to gain then test (via assertion) the value of properties that should be initialized. When I have multiple initializers, I unit test them individually. Of course, if there is a failure in the accessors, these unit tests will indicate that the object isn't properly initialized, but still, catching bugs is what unit testing is all about. If you are just looking to ensure that the object was created at all, you can assert that it is not nil. You can, for instance, create assertions that invoke methods on the object (say, if you want to confirm its Class). What specifically about the object's initialization are you wanting to confirm? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Detecting Initialization
On Thu, Jan 29, 2009 at 11:39:21AM +1100, Graham Cox wrote: > > On 29 Jan 2009, at 11:06 am, David H. Silber wrote: > > >Is there some means of detecting if an instance has been initialized? > > > >I'm thinking not. Apple's "NSObject Class Reference" says "The init > >method defined in the NSObject class does no initialization", and > >googling around on the topic has not produced any means of doing > >so. I > >ask here in case someone has some knowledge I have not been able to > >find. Even a definitive "No" would help, as I could stop searching > >for > >an answer which doesn't exist. > > The answer is no, but in order to comply with Cocoa's non-optional > rule on this, if an instance exists, it's initialised. Otherwise > you've made a grave error. > > WAYTTD? I was hoping to be able to write a unit test which would confirm that an object had been properly initialized. Thanks, David -- David H. Silber ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Detecting Initialization
On 29 Jan 2009, at 11:06 am, David H. Silber wrote: Is there some means of detecting if an instance has been initialized? I'm thinking not. Apple's "NSObject Class Reference" says "The init method defined in the NSObject class does no initialization", and googling around on the topic has not produced any means of doing so. I ask here in case someone has some knowledge I have not been able to find. Even a definitive "No" would help, as I could stop searching for an answer which doesn't exist. The answer is no, but in order to comply with Cocoa's non-optional rule on this, if an instance exists, it's initialised. Otherwise you've made a grave error. WAYTTD? --Graham ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Detecting Initialization
On 28 Jan 09, at 16:06, David H. Silber wrote: Is there some means of detecting if an instance has been initialized? No. However, this shouldn't ever be necessary - there should never be a situation where you're passing around uninitialized objects. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com