warning when initializing a variable

Discussion in 'Mac Programming' started by Chirone, May 27, 2009.

  1. Chirone macrumors 6502

    Mar 2, 2009
    i get this warning
    warning: initialization from distinct Objective-C type

    i googled this and i found out that it's caused when the returning type is no the same as the type it's being assigned too

    however how is this possible when the two types match?

    the initializer is like this:
    -(ObjectOne*) initWithString: (NSString*) s {
        self = [super init];
        if(self) {
        return self;
    the making of an instance is like this:
    ObjectOne* oo = [[ObjectOne alloc] initWithString: @"bleh"];
    it's no different to any other initializing code yet it generates warnings and things that should happen never do... yet the object still exist when initialized....
  2. jpyc7 macrumors 6502

    Mar 8, 2009
    Denver, CO
    I could be totally wrong as I don't know Obj-C, but maybe the @"bleh" is a const NSString? I don't know if a "const" would generate that warning.

    Another thing to try is to split the alloc to temporary variable and then call the initWithString (on separate lines) to see if you only get the warning on one line.
  3. HiRez macrumors 603


    Jan 6, 2004
    Western US
    Normally you return type id from initializers, not a static type. Try this:

    - (id)initWithString:(NSString*) s {
    What kind of object is ObjectOne? A subclass of NSObject?
  4. Chirone thread starter macrumors 6502

    Mar 2, 2009
    thanks jpyc7, sorry, i didn't try your second suggestion, i don't think it would have worked if i did any of those anyway.
    i didn't think that passing a literal would make a difference as it never has before...

    HiRez, thanks... it's strange though, because i've always done it that way and it's never caused warnings or errors :confused: if it comes up again i'll try returning an id type instead (or am i meant to return a type id anyway? the first tutorial i ever saw never did.... so i assumed i didn't have to)
    ObjectOne is a subclass of NSObject

    the now confusing thing is... after compiling a lot of times and playing with the code inside ObjectOne's implementation file the warning disappeared...
    did i alter the constructor method? NOPE
    am i mystified by it? YES

    very very odd.....

Share This Page