- (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;
}
a source to share
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.
a source to share