Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deprecate a method in Xcode

We have our library we ship to our customers, and I'd like to mark some methods as "deprecated" because we changed them (like Apple does in the iPhone SDK).

I've seen the __OSX_AVAILABLE_BUT_DEPRECATED pre-processor macro, which is mapped to __AVAILABILITY_INTERNAL, which is mapped to __attribute__((deprecated))...

Well i'm a bit confused with this stuff!

Anybody knows something about that ?

like image 343
Julien Avatar asked Feb 07 '11 17:02

Julien


People also ask

Where is deprecated methods in Xcode?

Navigate to Issue Navigator. In the Issue navigator, type "deprecated" in the below filter. Now you will be able to see all (and only) deprecated Buildtime warnings.

What does method deprecated mean?

Similarly, when a class or method is deprecated, it means that the class or method is no longer considered important. It is so unimportant, in fact, that it should no longer be used at all, as it might well cease to exist in the future. The need for deprecation comes about because as a class evolves, its API changes.

What does deprecated mean in Swift?

Deprecated methods or classes that are outdated one which will eventually be removed.


4 Answers

__attribute__((deprecated)) is the gcc way (also supported in clang) of marking a function / method as deprecated. When one is marked as "deprecated", a warning will be produced whenever anyone calls it.

The syntax for normal functions would be

__attribute__((deprecated))
void f(...) {
  ...
}

// gcc 4.5+ / clang
__attribute__((deprecated("g has been deprecated please use g2 instead")))
void g(...) {
  ...
}

and that of Objective-C methods would be

@interface MyClass : NSObject { ... }
-(void)f:(id)x __attribute__((deprecated));
...
@end

You can also mark the whole class as deprecated with

__attribute__((deprecated))
@interface DeprecatedClass : NSObject { ... }
...
@end

Apple also provides the <AvailabilityMacros.h> header which provides the DEPRECATED_ATTRIBUTE and DEPRECATED_MSG_ATTRIBUTE(msg) macros that expand to the above attributes, or nothing if the compiler doesn't support attributes. Note that this header doesn't exist outside of OS X / iOS.


Side note, if you are using Swift you use the @available attribute to deprecate an item, e.g.

@available(*, deprecated=2.0, message="no longer needed")
func f() {
    ...
}
like image 164
kennytm Avatar answered Oct 14 '22 05:10

kennytm


You can also use more readable define DEPRECATED_ATTRIBUTE

It defined in usr/include/AvailabilityMacros.h:

#define DEPRECATED_ATTRIBUTE        __attribute__((deprecated))
#define DEPRECATED_MSG_ATTRIBUTE(msg) __attribute((deprecated((msg))))

Objective-C methods example:

@interface MyClass : NSObject { ... }
-(void)foo:(id)x DEPRECATED_ATTRIBUTE;

// If you want to specify deprecated message:
-(void)bar:(id)x DEPRECATED_MSG_ATTRIBUTE("Use baz: method instead.");
...
@end

You can also mark the whole class as deprecated:

DEPRECATED_ATTRIBUTE
@interface DeprecatedClass : NSObject { ... }
...
@end
like image 22
skywinder Avatar answered Oct 14 '22 04:10

skywinder


Swift 5.0

Deprecate any method/class/struct/protocols using @available

@available(*, deprecated, message: "Parse your data by hand instead")
func parseData() { }

@available(*, deprecated, renamed: "loadData")
func fetchData() { }

@available(swift, obsoleted: 4.1, renamed: "attemptConnection")
func testConnection() { }

@available(swift, deprecated: 4.0, obsoleted: 5.0, message: "This will be removed in v5.0; please migrate to a different API.")

Possible params:

  • introduced
  • deprecated
  • obsoleted
  • message
  • renamed

For more info see apple doc: Attributes

like image 30
Lal Krishna Avatar answered Oct 14 '22 04:10

Lal Krishna


If you are using C++14 in your xcode project, you may also use the [[deprecated]] or [[deprecated("reason")]] attribute that is now part of the language.

see documentation: http://en.cppreference.com/w/cpp/language/attributes

like image 33
Ant6n Avatar answered Oct 14 '22 06:10

Ant6n