-1

I'm working on exercise 3 on page 76 in Apple's developer pdf in the class categories and extensions section, "Programming with Objective C", found here: (https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html)

my XYZPerson header looks like this:

#import <Foundation/Foundation.h>

@interface XYZPerson : NSObject

@property (readonly) NSNumber *height;
@property (readonly) NSNumber *weight;

-(NSNumber *)measureHeight;
-(NSNumber *)measureWeight;

@end

My implementation file looks like this:

#import "XYZPerson.h"

@property (readwrite) NSNumber *height;
@property (readwrite) NSNumber *weight;

@end
/////////////////////

@implementation XYZPerson

-(NSNumber *)measureHeight{
    if(!_height){
        _height = [[NSNumber alloc] init];
    }
    return _height;
}

-(NSNumber *)measureWeight{
    if(!_weight){
        _weight = [[NSNumber alloc] init];
    }
    return _weight;
}

@end

And then in my main file I have:

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

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

    @autoreleasepool {
        XYZPerson *aPerson = [[XYZPerson alloc] init];

        [aPerson setHeight: [NSNumber numberWithInt:72] ];
        [aPerson setWeight: [NSNumber numberWithInt:190] ];
        NSLog(@"%@ %@",[aPerson measureHeight],[aPerson measureWeight]);
    }
    return 0;
}

There might be more than one error unrelated to the issue I brought up, I'm a huge novice at Objective C right now. The exact compiler error I am getting is on the line that says,

[aPerson setHeight: [NSNumber numberWithInt:72] ];

The compiler error reads, "ARC Semantic Issue. No visible @interface for 'XYZperson' declares the selector 'setWeight:'.

mepstein1218
  • 331
  • 5
  • 14
  • I am a bit confused, my problem is I can't figure out how to implement extensions correctly. In all the guides I've seen, they say that you can have a readonly property in your header file, then in the implementation file include an extension and overwrite the readonly property with a readwrite property. – mepstein1218 Sep 04 '14 at 20:03
  • You can override the `readonly` property to `readwrite` **for internal use only** using a class extension. If you want the property to be `readwrite` for external callers, then you should just change the declaration in the header. For that case, there's no reason to declare it one way in one place and then override it in another. – Ken Thomases Sep 04 '14 at 22:48

4 Answers4

0

You declared the property read-only in the header file, so to everything outside of the class, the properly is read-only, and thus a setHeight: method doesn't exist.

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • OK, if I have a readonly property in my header file though, how do I change that into readwrite using a class extension? – mepstein1218 Sep 04 '14 at 19:17
0

You have made he properties height and weight read only. Then in the main function you are using setWeight and setHeight.

You can't do that as that would be writing the weights but you explicitly set them to read only.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
0

Oh doh.

You will not be able to call that method from OUTSIDE of your class implementation. Overriding properties in class extensions in the .m file doesn't expose that to the world. At least not to the compiler.

That's the whole point. It's for when you want properties that are readonly to the world outside of that object, but internally to the object you still want to be able to conveniently do things.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
  • I actually have the '@interface XYZPerson ()' declaration in my implementation, I just have forgotten to include it here. Thanks for looking at my code though, sorry for all the errors I made on stackexchange. So this is still pretty confusing because it looks like I have everything people are suggesting. – mepstein1218 Sep 06 '14 at 15:37
  • see my change. I got it now. You're calling these outside the class implementation, the compiler enforces the interface. – uchuugaka Sep 06 '14 at 15:57
  • Thank you, I think I understand now! Thanks for bearing with me, you were very helpful. – mepstein1218 Sep 06 '14 at 16:43
0

Place your properties in .m with class extension:

@interface XYZPerson ()

@property (readwrite) NSNumber *height;
@property (readwrite) NSNumber *weight;

@end

See Apple docs about class extension: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html

Hoogle
  • 413
  • 3
  • 10