Using #pragma to suppress “Instance method not found” warnings in Xcode

前端 未结 5 830
野性不改
野性不改 2020-12-15 13:23

I want to use #pragma (in Xcode) to suppress the warning:

warning: instance method \'-someMethod\' not found (return type defaults to \'id\'

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 13:27

    OK, me again :) I tried various 'clang' specific #pragmas but nothing worked, and only thing I could think of was to declare the method as a 'private method' from within the class that actually uses the method, thus:

    main.m:

    #import 
    #import "PragmaTest.h"
    
    @interface PragmaTest ()
    
        - (void)noSuchMethod;
    
    @end
    
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
    
            PragmaTest *pragmaTest = [[PragmaTest alloc] init];
    
            #pragma clang diagnostic push
            #pragma clang diagnostic ignored "-Wall"
    
            [pragmaTest noSuchMethod];
    
            #pragma clang diagnostic pop
    
            [pragmaTest release];
    
            // insert code here...
            NSLog(@"Hello, World!");
    
        }
        return 0;
    }
    

    PragmaTest.h:

    #import 
    
    @interface PragmaTest : NSObject
    
    @end
    

    PragmaTest.m:

    #import "PragmaTest.h"
    
    @implementation PragmaTest
    
    - (void)noSuchMethod
    {
        NSLog(@"noSuchMethod");
    }
    
    @end
    

    I hope this meets your requirements and although it's not a solution involving #pragmas I hope you won't feel the need to downvote a helpful answer.

提交回复
热议问题