Home  >  Q&A  >  body text

ios - OC为什么类不写实现,突然报了的编译错误?

昨天的测试工程,代码来自斯坦福CS193P,一开始一点问题没有。今天试试selector 突然就编译不了了。即使注释的就剩ALLOC的那一行代码。究竟是什么问题啊。
奇怪的是补上实现后,又不报错了。

#import <Foundation/Foundation.h>
@interface Vehicle :NSObject
- (void)move;
@end

//!!!!添加实现后build succeeded!!!!
//@implementation Vehicle
//
//@end

@interface Ship : Vehicle
- (void)shoot;
@end


//@implementation Ship
//- (void)shoot {
//    NSLog(@"Shoot!");
//}
//@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //-------------code 2---------------
        Ship *s = [[Ship alloc] init];
        [s shoot];
        [s move];
        
        //Vehicle *v = shipWithShoot;
        //[v shoot];
        
//        id obj = shipWithShoot; //s is Ship
//        [obj shoot];
        //obj = v;    //v is Vehicle
//        [obj shoot];
        
        //-------------code 4----------link 2
//        if ([obj respondsToSelector:@selector(shoot)]) {
//            [obj shoot];
//        }

//
//        if ([obj isKindOfClass:[NSString class]]) {
//            NSString *str = [(NSString *)obj stringByAppendingString:@"xyzzy"];
//        }
        //-------------code 3---------------
        ...
    }
    
    return 0;
}

伊谢尔伦伊谢尔伦2713 days ago494

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-17 17:23:50

    I have never encountered this situation. Personally, I think the possible cause is a problem of repeated compilation. For specific details and solutions, you can refer to this blog to completely solve the _OBJC_CLASS_$_certain file name", referenced from: problem. I hope it can help you

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:23:50

    Of course, the header file only declares symbols. If there are only symbols but no implementation, it will report that the symbol cannot be found when linking

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 17:23:50

    It prompts that it cannot be found. If you add the implementation, there will be no problem. As for the problem, it may be a problem with the compiler.

    reply
    0
  • Cancelreply