Getting an error when executing the properties example


#1

i updated the person.h file as follows:

@interface Person : NSObject

// we will assign it two instance variables

{
float heightinmeters;
int weightinkilos;
}



@property float heightinmeters;
@property int weightinkilos;

- (float) bodymassindex;


@end

and updated person.m

#import "Person.h"

@implementation Person

@synthesize heightinmeters, weightinkilos;

- (float) bodymassindex
{
    return weightinkilos / (heightinmeters * heightinmeters);
}

@end

but in my main, i get error message resulting in build failure

 @autoreleasepool {
        
      // create an instance of the new Person class
        
        Person *person = [[Person alloc] init];
        
        // give the instance varibales values
        
        [person setweightinkilos:96];
        [person setheightinmeters:1.8];
        
        // now call the bmi method
        
        float bmi = [person bodymassindex];
        
        NSLog(@"person (%d, %f) has BMI of %f", [person weightinkilos],[person heightinmeters], bmi);
                           
                           
    }
    return 0;
}

error messages are specific to “Receiver type ‘Person’ for instance message does not declare a method with selector ‘setweightinkilos:’”


#2

Objective-C is case sensitive.


#3

Aaron, thanks for taking the time to look, but while I fully admit to being an idiot, I cannot find any case-sensitive inconsistencies in the code I’ve provided. The exampes worked fine for me until I made the changes to accommodate properties in the .h and .m files.

Perhaps I have a concussion from banging my head on the desk, but I’m just not seeing the problem.

thanks


#4

If you are going to name your variable “heightinmeters” (which I really wish you would not – camel-case is how we roll in the community), the setter method is setHeightinmeters:.


#5

ahhh, syntax can be such a finicky thing! :confused:

thanks for your patience. AndCamelCaseItIsFromHereOnOut.

as an FYI, great book. i’ve been sailing thru pretty good until this tumble.
peace.


#6

I ran into the same error. Just to be sure I understand: “set” is a method followed by a variable in uppercase (“setHeightInMeters”), even if the variable is declared lower-case (“float heightInMeters;”)?
I had written out “setheightInMeters” mistakenly thinking that case sensitivity would cause an error.


#7

That’s exactly right: It capitalizes the variable name, prepends “set” and appends “:”. That is the setter method that gets created.

(Trivia: it is also the setter method that key-value coding will call if it exists.

[myFriend setValue:@"Bob" forKey:@"firstName"];
results in myFriend’s setFirstName: method being called.)