Having trouble with class extension in Objective C, mac OS. Getting error 'NSInvalidArgumentException', no visible @interface declares the selector

痞子三分冷 提交于 2019-12-12 06:57:27

问题


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:'.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/25672025/having-trouble-with-class-extension-in-objective-c-mac-os-getting-error-nsinv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!