4

setting any method to deprecated is as easy. See How do I flag a method as deprecated in Objective-C 2.0?

BUT: How can I set a method deprecated only for public use?

Community
  • 1
  • 1
martn_st
  • 2,576
  • 1
  • 24
  • 30
  • I mainly use the `deprecated` flag for my refactoring. In that particular case I was asking for I have one method I wanted to keep for internal use in my class. At the same time it should not be used publicly anymore because I provided a improved one. But since it works a bit different I couldn't just use it everywhere right away. – martn_st Feb 24 '14 at 09:00

2 Answers2

3

Another choice is to add a macro that is defined in your build flags and not defined in theirs.

// 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
Greg Parker
  • 7,972
  • 2
  • 24
  • 21
  • Thanks for you answer. Also an interesting approach. But in my case I am not actually working framework like project. See my additional comment on the question. – martn_st Feb 24 '14 at 08:58
1

you could define it in two headers.. in two different categories.

dont define it on the class itself.

that way you separate them


e.g. you have:

a class T that main is using

T has a deprecated property. But internally you want to use it

so to 'clients' you expose it as deprecated in a Category while the private m file just declares it itself not-deprecated

main file : (client)

#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

and T Public

#import "T.h"

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

and Finally T.m that DOESNT import the public interface

#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
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45468/discussion-between-daij-djan-and-trojanfoe) – Daij-Djan Jan 17 '14 at 12:52
  • Thanks for your (in my opinion the best) answer / solution. But unf. it is still a workaround for something Object-C probably just does not support. For my case it might be little bit to complex to have a separate interface file. Also to keep in mind that it requires to import a specific header file for using the class can get confusing in future for by collaborating developers and maybe even myself. _However_ I think it is a nice and easy to understand solution. – martn_st Feb 24 '14 at 09:18
  • 1
    I agree that it introduces complexity and it is only a workaround :) – Daij-Djan Feb 24 '14 at 09:26