在Mac OS的Objective C中遇到类扩展问题.收到错误"NSInvalidArgumentException",没有可见的@interface声明选择器 [英] Having trouble with class extension in Objective C, mac OS. Getting error 'NSInvalidArgumentException', no visible @interface declares the selector

查看:48
本文介绍了在Mac OS的Objective C中遇到类扩展问题.收到错误"NSInvalidArgumentException",没有可见的@interface声明选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Apple开发人员pdf中第76页上的练习3,该类位于类别和扩展部分使用Objective C编程",位于:(

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)

我的XYZPerson标头看起来像这样:

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

我的实现文件如下:

#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;
}

可能有多个错误与我提出的问题无关,我现在是Objective C的一名新手.我得到的确切编译器错误是在显示

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] ];

编译器错误显示为"ARC语义问题.'XYZperson'的不可见@interface声明了选择器'setWeight:'.

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

推荐答案

哦.

您将无法从类实现的外部调用该方法..m文件中的类扩展名中的重写属性不会将其公开.至少不是编译器.

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.

这篇关于在Mac OS的Objective C中遇到类扩展问题.收到错误"NSInvalidArgumentException",没有可见的@interface声明选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆