Когда я пишу свою первую демонстрацию 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;
}

У меня всегда бывает исключение:

Неперехваченное исключение NSInvalidArgumentException, причина: Человек (экземпляр) не распознает 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.

0
Kyle Wang 18 Мар 2021 в 10:36

1 ответ

Лучший ответ

Проблема в том, что вы используете «онлайн-компиляторы», которые ничего не знают о функциях 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;
}

Это компилируется и работает, как ожидалось.

1
matt 18 Мар 2021 в 18:24