Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Basic Objective C question?

I'm learning Objective C and I wanted to make a super simple program for some practice. I get the error message "no getter method for read from property." What can I do to fix this? Here's my program so far:

#import <Foundation/Foundation.h>

//----Interface----

@interface Person: NSObject

- (void) setHeight: (double) h;

- (void) setWeight: (double) w;

@end

//----Implementation

@implementation Person

{

double height;

double weight;

}

- (void) setHeight:(double)h;

{

height = h;

}

- (void) setWeight:(double)w;

{

weight = w;

}

@end

//----Program----

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

{

@autoreleasepool {

Person *p1 = [[Person alloc] init];

[p1 setHeight: 5.9];

[p1 setWeight:165];

NSLog (@"Person is %f feet tall and weighs %f LBS", p1.height, p1.weight); // FIXME

}

return 0;

}

Update:

Also, is there an equivalent to c++'s "this->" operator in objective c?

2 Answers

Relevance
  • 7 years ago
    Favorite Answer

    Hello there, here is correct code for you problem.

    #import <Foundation/Foundation.h>

    //----Interface----

    @interface Person: NSObject

    @property (nonatomic) double weight; // creates both, setter and getter for "weight" property (variable)

    @property (nonatomic) double height; // same as above but for "height"

    @end

    //----Program----

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

    {

    @autoreleasepool {

    Person *p1 = [[Person alloc] init];

    [p1 setHeight:5.9];

    [p1 setWeight:165];

    NSLog (@"Person is %f feet tall and weighs %f LBS", p1.height, p1.weight); // FIXME

    }

    return 0;

    }

    In Objective-C we are working with properties (similar to variables). Setter and getter is created automatically for you when you create new property (however if you want you can still write your own). This is why in this case you do need to write @implementation at all because all you need there is setter (to set values) and getter (to read the current value <-- you was missing this). In your NSLog code you want to READ current values of your variables (properties) so you need to gave getters to GET your current values. This is why you had a compiler error in that line of code.

    You can read more here: https://developer.apple.com/library/mac/documentat...

  • 7 years ago

    Not sure, but you might have been able to do this without getter functions by using the pointer accessor ->

    NSLog (@"Person is %f feet tall and weighs %f LBS", p1->height, p1->weight);

    it sort of defeats the purpose of Objective C though. At that point you are coding in C.

    Remember also, that properties are the basis of Key-Value Coding (KVC), which is the mechanism in Objective C for implementing bindings between objects and their data. This embedded mechanism relies on specific names for the getter and setter functions, which is why they are automatically created when declaring a property. If you write your own methods you must adhere to the proper naming convention.

    For the property "foo" the setter should be "setFoo", and the getter "foo" . Basically the getter name is just the name of the variable.

Still have questions? Get your answers by asking now.