views:

40

answers:

1

I have been a Java programmer for years but only iPhone/Obj-c for a few months. Every time I think I'm comfortable with the language something weird happens. Why does the following generate a "Incompatible types in initialisation" compile error? It seems so straight forward. 'double' is a primitive right?!?

-(void) testCalling{
   double myDoub = [self functionReturningDouble:3.0];
}


-(double) functionReturningDouble:(double) input{
   return 1.0;
}
A: 

Try to swap the method declarations. It may be a scope problem as Georg notices:

-(double) functionReturningDouble:(double) input{
    return 1.0;
}

-(void) testCalling{
    double myDoub = [self functionReturningDouble:3.0];
}

In Objective-C (and this is valid for C), a method does "exists" only if it has been defined or declared before.

Laurent Etiemble
Rather, a methods signatures is only known in those cases :)
Georg Fritzsche
Thankyou! Can't believe it took this long to discover this.
Mike Simmons
Just another point.. out of interest why does the order not seem to matter with a int method but only with double?
Mike Simmons
This is because the default return type for an undefined function is "int".
Laurent Etiemble