执行代码时得到错误"Person(instance) does not recognize setName"

Get the error "Person(instance) does not recognize setName" when code executed

当我编写第一个 Objective-C 演示时,

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property (assign) NSString *name;
- (void)display;
@end

@implementation Person
- (void)display{
    NSLog(@"Name is %@", self.name);
}
@end

int main() {
    Person *p = [[Person alloc] init];
    p.name = @"Kyle";
    [p display];
    return 0;
}

我总是遇到异常:

Uncaught exception NSInvalidArgumentException, reason: Person(instance) does not recognize setName:

此外,我总是收到一些奇怪的警告,例如incomplete implementation of class ‘Person’

这里是详细输出:

/usercode/file.m:13:1: warning: incomplete implementation of class ‘Person’ [enabled by default]
 @end
 ^
/usercode/file.m:13:1: warning: method definition for ‘-setName:’ not found [enabled by default]
/usercode/file.m:13:1: warning: method definition for ‘-name’ not found [enabled by default]
2021-03-18 07:28:46.540 a.out[11] autorelease called without pool for object (0x84a2f8) of class NSMethodSignature in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.556 a.out[11] autorelease called without pool for object (0x8d5168) of class NSMutableDataMalloc in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.556 a.out[11] autorelease called without pool for object (0x84b3e8) of class GSCodeBuffer in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.556 a.out[11] autorelease called without pool for object (0x8d5288) of class NSMethodSignature in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.556 a.out[11] autorelease called without pool for object (0x8d08f8) of class GSFFIInvocation in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.556 a.out[11] autorelease called without pool for object (0x8d0278) of class GSCInlineString in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.556 a.out[11] autorelease called without pool for object (0x8d5138) of class NSException in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.563 a.out[11] autorelease called without pool for object (0x8c62f8) of class NSLongLongNumber in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.563 a.out[11] autorelease called without pool for object (0x8c6d68) of class NSLongLongNumber in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.563 a.out[11] autorelease called without pool for object (0x8d24e8) of class NSLongLongNumber in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.563 a.out[11] autorelease called without pool for object (0x8c4e38) of class NSLongLongNumber in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.563 a.out[11] autorelease called without pool for object (0x8d2e28) of class NSLongLongNumber in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.563 a.out[11] autorelease called without pool for object (0x8d2648) of class NSLongLongNumber in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.563 a.out[11] autorelease called without pool for object (0x8d0718) of class NSLongLongNumber in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.563 a.out[11] autorelease called without pool for object (0x7ccff8) of class NSIntNumber in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.563 a.out[11] autorelease called without pool for object (0x874808) of class NSLongLongNumber in thread <NSThread: 0x7ee668>
2021-03-18 07:28:46.563 a.out[11] autorelease called without pool for object (0x7fcc18) of class NSIntNumber in thread <NSThread: 0x7ee668>
/usercode/a.out: Uncaught exception NSInvalidArgumentException, reason: Person(instance) does not recognize setName:

谁能帮我解决这个问题? TIA.

问题是您使用的“在线编译器”对 Objective-C 2.0 功能一无所知,例如您的代码所依赖的自合成属性。如果您的目标是学习 Xcode 使用的 Objective-C,那么使用 Xcode 会简单得多。让我们把它变成一个命令行工具 Xcode;这个文件现在是 main.m...

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property (assign) NSString *name;
- (void)display;
@end

@implementation Person
- (void)display{
    NSLog(@"Name is %@", self.name);
}
@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *p = [[Person alloc] init];
        p.name = @"Kyle";
        [p display];
    }
    return 0;
}

按预期编译和运行。