Re: On self = [super init...] ...
Re: On self = [super init...] ...
- Subject: Re: On self = [super init...] ...
- From: Thomas Davie <email@hidden>
- Date: Thu, 18 Nov 2010 21:40:53 +0000
On 18 Nov 2010, at 21:16, Bill Bumgarner wrote:
>
> On Nov 18, 2010, at 1:10 PM, John Engelhart wrote:
>
>> The basic premise behind self = [super init...] is that the "lower levels of initialization are free to return a different object than the one passed in".
>>
>> However, there is an unstated assumption in this reasoning: whatever object is returned by [super init...] is the one that will be used.
>
>
> I don't understand the above claim; Why must the object returned by [super init*] be the one that is used?
>
> I'm certainly not aware of any limitation on an init method that would prevent it from… say… calling [super init], releasing whatever is returned if deemed unfit and then allocating an initializing some other instance.
>
> The documentation (http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/ObjectiveC/Articles/ocAllocInit.html) doesn't seem to make any such claim either.
>
> I haven't read beyond the above yet. Maybe the unstated assumption is explained?
Indeed, the behaviour of immutable strings and containers seem to back up this assertion
#import <Foundation/Foundation.h>
int main (int argc, char ** argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *s1 = @"";
NSString *s2 = [NSString stringWithString:s1];
NSString *s3 = [NSString alloc];
NSString *s4 = [s3 initWithString:s2];
NSLog(@"%p, %p, %p, %p", s1, s2, s3, s4);
[pool drain];
}
prints 2010-11-18 21:39:35.767 Test[18767:a0f] 0x100001050, 0x100001050, 0x10010d470, 0x100001050
So our allocated string is certainly substituted during initialisation.
Bob_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden