Reputation: 5590
I'm trying to return a double from another object then store that into a new double but i'm getting the error incompatible types in 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;
}
Upvotes: 2
Views: 1682
Reputation: 64428
The getter/setter kvo standard defines getters in the form of getPropertyName
. If you have a property called gradePoints
the compiler will interpret getGradePoints
as the getter for that property and if the property is not defined as a double, it will complain.
Even defining a local variable like this:
double gradePoints = 0.0;
double other = [aCourse getGradePoints];
... may confuse the compiler because it may try to process getGradePoints
as the getter for gradePoints.
Objective-C relies on naming conventions to find specific types of methods because it can't assume at compile time what methods an object in a particular circumstance will have.
In general you should avoid using method names that begin 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 try to use prefixes like "fetch", "grab", "obtain" etc instead of "get" just to be safe.
Upvotes: 0
Reputation: 29019
Most likely you have forgotten to add the getGradePoints method to an interface declaration: This will result in the method being implicitly declared as -(id)getGradePoints; resulting in the warning you are seeing.
Upvotes: 7
Reputation: 36041
Is the reference to aCourse
typed or is it an id
? If I remember correctly, if the class of aCourse
isn't known to the compiler, it assumes that the result of all method calls is type id
.
Upvotes: 2