PDA

View Full Version : warning when initializing a variable




Chirone
May 27, 2009, 05:00 PM
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....
:confused:



jpyc7
May 27, 2009, 09:17 PM
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 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....
:confused:

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.

HiRez
May 27, 2009, 10:40 PM
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?

Chirone
May 27, 2009, 11:28 PM
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.....