How to set methods that are deprecated only in the open interface in Objective-C

setting any method to obsolete is simple. See How can I mark a method as deprecated in Objective-C 2.0?

BUT: How to install a method that is deprecated only for general use?

+4
source share
2 answers

Another option is to add a macro that is defined in your build flags and not defined in them.

// Add -DBUILDING_MYPROJECT=1 to your own build flags. 
#if BUILDING_MYPROJECT
#   define MYPROJECT_DEPRECATED_API
#else
#   define MYPROJECT_DEPRECATED_API DEPRECATED_ATTRIBUTE
#endif
...    
-(void) method  MYPROJECT_DEPRECATED_API;  // deprecated for clients, not deprecated for you
+3
source

you can define it in two headings .. in two different categories.

Do not define it in the class itself.

so you separate them


eg. you have:

a class T that main is using

T has an obsolete property. But internally you want to use it

"" , m

: ()

#import <Foundation/Foundation.h>
#import "T+Public.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        T *t = [[T alloc] init];
        NSLog(@"%@", t.deprecated);

    }
    return 0;
}

T.h

#import <Foundation/Foundation.h>

@interface T : NSObject
@end

T Public

#import "T.h"

@interface T (Public)
@property(nonatomic, readonly, copy) NSString *deprecated DEPRECATED_ATTRIBUTE;
@end

T.m, DOESNT

#import "T.h"

@interface T ()
@property(nonatomic, copy) NSString *deprecated;

@end
@implementation T

- (id)init {
    self = [super init];
    self.deprecated = @"LALA";
    NSLog(@"%@", self.deprecated); //NOT DEPRECATED!
    return self;
}

@end
+1

All Articles