How do I flag a method as deprecated in Objective-C 2.0?

前端 未结 5 438
天命终不由人
天命终不由人 2020-12-07 08:18

I\'m part of a team developing a fairly large iPad app and there are many different classes we\'ve created as a result. The trouble is some of the methods are now pretty mu

相关标签:
5条回答
  • 2020-12-07 08:39

    If you want to give additional message with the deprecation flag, you can use following flags.

    @property (strong, nonatomic) NSString *catName
                        __deprecated_msg("use name instead.");
    
    //  -- Or -- 
    @property (strong, nonatomic) NSString *catName
                        DEPRECATED_MSG_ATTRIBUTE("use name instead.");
    
    //  -- Or -- 
    @property (strong, nonatomic) NSString *catName
                        __attribute__((deprecated("use name instead.")));
    

    Using above mentioned flags, you can tell why you are deprecating or what is the method developer should use in future.

    0 讨论(0)
  • 2020-12-07 08:42

    To mark a method as deprecated, use __attribute__((deprecated("Your message goes here")))

    A practical example, from Mantle

    @interface NSValueTransformer (UnavailableMTLPredefinedTransformerAdditions)
    
    + (NSValueTransformer *)mtl_externalRepresentationTransformerWithModelClass:(Class)modelClass __attribute__((deprecated("Replaced by +mtl_JSONDictionaryTransformerWithModelClass:")));
    + (NSValueTransformer *)mtl_externalRepresentationArrayTransformerWithModelClass:(Class)modelClass __attribute__((deprecated("Replaced by +mtl_JSONArrayTransformerWithModelClass:")));
    
    @end
    
    0 讨论(0)
  • 2020-12-07 08:46

    Use the deprecated attribute:

    - (int)bar: (int)x __attribute__((deprecated));
    
    0 讨论(0)
  • 2020-12-07 08:48

    IMHO, it's easier to write __deprecated:

    - (void)myDeprecatedMethod __deprecated;
    - (int)methodNameDeprecated:(int)param __deprecated;
    

    Works too on classes

    __deprecated
    @interface MyDeprecatedClass
    
      // ... some properties and methods ...
    
    @end
    
    0 讨论(0)
  • 2020-12-07 08:56

    Deprecation Syntax

    Syntax is provided to mark methods as deprecated:

    @interface SomeClass
    -method __attribute__((deprecated));
    @end
    

    or:

    #include <AvailabilityMacros.h>
    @interface SomeClass
    -method DEPRECATED_ATTRIBUTE;  // or some other deployment-target-specific macro
    @end
    
    0 讨论(0)
提交回复
热议问题