- (double) valid return type for a method in Objective C

I am trying to return a double from another object and then store it into a new double, but I am getting an error in incompatible types on initialization. What am I missing here?

double gradePoints = 0.0;
double other = [aCourse getGradePoints];
gradePoints = gradePoints + other;

      

This is in my other object

- (double) getGradePoints{
   return 12.0;
 }

      

+2


a source to share


3 answers


Most likely, you forgot to add the getGradePoints method to the interface declaration: this will cause the method to be implicitly declared as (id) getGradePoints; the result is the warning you see.



+7


a source


Is the link to aCourse

or is it id

? If I recall correctly, if the class is aCourse

not known to the compiler, it assumes that the result of all method calls is a type id

.



+2


a source


The kver standard getter / setter defines getters in the form getPropertyName

. If you have a property called gradePoints

, then the compiler interprets getGradePoints

as a getter for that property, and if the property is not defined as a double, it will complain.

Even defining a local variable:

double gradePoints = 0.0;
double other = [aCourse getGradePoints];

      

... can be confusing to the compiler because it might try to process getGradePoints

as a receiver for class points.

Objective-C relies on naming conventions to find specific types of methods because it cannot take into account at compile time what methods the objects will have in a particular case.

In general, you should avoid using method names that start with "get" and "set" because the compiler wants to treat them as getter and setter methods for properties. The potential for compiler confusion is high.

I am trying to use prefixes like "fetch", "capture", "get", etc. instead of "get" to be safe.

0


a source







All Articles