Why do I need to enter self in id?

I have an init method that takes an argument (id):


    -(id) initWithObject:(id) obj;

I try to call it this way:


    [[MyClass alloc] initWithObject:self];

But Xcode complains that the argument is a “separate Objective-C type” (which usually indicates a type mismatch or level of indirectness error).

If I explicitly included myself (id), the warning will disappear. In any case, the code works as expected. Interestingly, on the next line, I pass myself to another method that also takes an identifier, and that works fine.

I am wondering if I am missing something subtle - or is it a feature of the compiler?

It’s not very convenient for me to just drop it until I’m sure of the reasons why this is necessary.

[change]

. , . , . , init. initWithSource, :


-(id) initWithFrame:(CGRect) frame
{
    self = [super initWithFrame: frame];
    if( self )
    {
        delegate = nil;
        touchDelegate = [[TBCTouchDelegate alloc] initWithSource:self];
        [touchDelegate.viewWasTappedEvent addTarget: self action:@selector(viewWasTapped:)];
    }
    return self;
}

init:


-(id) initWithSource:(id) sourceObject
{
    self = [super init];
    if (self != nil) 
    {
        // Uninteresting initialisation snipped
    }
    return self;
}
+5
1

, initWithSource: . , id, , . , initWithSource: id -typed, initWithSource:, , , . "", "

? 100%, , +[TBCTouchDelegate alloc] id. , alloc/init :

id o = [TBCTouchDelegate alloc];
touchDelegate = [o initWithSource:self];

, initWithSource: id -typed. initWithSource:, .

? , NSAppleScript:

- (id)initWithSource:(NSString *)source;

NSAppleScript , , iPhone. , , , ?

, , , alloc/init :

touchDelegate = [TBCTouchDelegate alloc];
touchDelegate = [touchDelegate initWithSource:self];

initWithSource: ( id -typed), , . +alloc:

touchDelegate = [(TBCTouchDelegate *)[TBCTouchDelegate alloc] initWithSource:self];

initWithSource:, , , . , , "", - .

+7

All Articles