Christian Rosick
Christian Rosick

Reputation: 41

instance method not found (return type defaults to 'id'

I wrote two programs during a training. But one exercise task drives me crazy. Not the task itself but the program and the behavior of it.

The first program is to calculate the BMI of a person. Here it works all fine.

main.m

#import <Foundation/Foundation.h>
#import "Person.h"

int main (int argc, const char * argv[])
{

@autoreleasepool {

    // Erstellt eine Instanz von Person
    Person *person = [[Person alloc]init];

    // Gibt den Instanzvariablen interessante Werte
    [person setWeightInKilos:93.2];
    [person setHeightInMeters:1.8];

    // Ruft die Methode bodyMassIndex auf
    float bmi = [person bodyMassIndex];
    NSLog(@"person (%dKg, %.2fm) has a BMI of %.2f", [person weightInKilos], 
[person heightInMeters], bmi);


}
return 0;
}

Person.h

@interface Person : NSObject
{
// Sie hat zwei Instanzvariablen
float heightInMeters;
int weightInKilos;
}

// Sie können diese Instanzvariablen anhand folgender Methoden setzen
@property float heightInMeters;
@property int weightInKilos;

// Diese Methode berechnet den Body-Mass-Index
- (float)bodyMassIndex;


@end

Person.m

#import "Person.h"

@implementation Person

@synthesize heightInMeters, weightInKilos;

- (float)bodyMassIndex
{
float h = [self heightInMeters];
return [self weightInKilos] / (h * h);
}
@end

This program was written by the author of the training.

My task is to write a program what is equal. In my opinion it looks exactly the same:

main.m

#import <Foundation/Foundation.h>
#import "StocksHolding.h"

int main (int argc, const char * argv[])
{

@autoreleasepool {

    StocksHolding *stock1 = [[StocksHolding alloc]init];

    [stock1 purchaseSharePrice:1]; 
/*Here I geht the error "Instance method '-purchaseSharePrice:' not found (return type
defaults to 'id')*/
    NSLog(@"%i", [stock1 purchaseSharePrice]);

}
return 0;
}

StockHoldings.h

#import <Foundation/Foundation.h>

@interface StocksHolding : NSObject
{
int purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
@property int purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

- (float)costInDollars;
- (float)valueInDollars;


@end

StockHoldings.m

#import "StocksHolding.h"

@implementation StocksHolding
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

- (float)costInDollars
{
return purchaseSharePrice * numberOfShares;
}
- (float)valueInDollars
{
return currentSharePrice * numberOfShares;
}


@end

AS you can see... almost no differences except the names of the variables and methods. Where is the error? I was sitting for 3 hours at this question.

Please give me a helping hand.

Thanks Christian

Upvotes: 1

Views: 1526

Answers (1)

Dima
Dima

Reputation: 23634

The problem is that you are not using the generated setter method.

purchaseSharePrice is a property.

The default setter is setPurchaseSharePrice: and the default getter is purchaseSharePrice.

So you can just do this

[stock1 setPurchaseSharePrice:1];

or this

stock1.purchaseSharePrice = 1;

In addition, when you want to get the value using the generated getter you can do

int myPrice = [stock1 purchaseSharePrice];

or this

int myPrice = stock1.purchaseSharePrice;

As you can see for both setting and getting, having a property allows you to use dot syntax with the property name directly, while using method syntax requires you to use the method names generated by the property.

Upvotes: 1

Related Questions