Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the unavailable attribute in Objective C mean?

What does the unavailable attribute in Objective C do?

__attribute__((unavailable("message")))

Is there any online reference to this and other attributes in Clang?

like image 544
cfischer Avatar asked Jun 23 '13 15:06

cfischer


People also ask

What are IOS attributes?

An attribute provides additional information about the declaration or type. For example, the discardableResult attribute on a function declaration indicates that, although the function returns a value, the compiler shouldn't generate a warning if the return value is unused.

What does asterisk mean in Swift?

An asterisk before a variable definition indicates that the variable is a pointer to an object. For example, let's consider the first line of code above: NSString *foo = [NSString stringWithUTF8String:"Hello World!"]; We're defining foo as a pointer to an object of type NSString.


1 Answers

The unavailable attribute marks a function declaration so that you can generate an error message if someone tries to use it. It's essentially the same as the deprecated attribute, except that trying to use a deprecated function just causes a warning, but using an unavailable one causes an error. Documentation at: http://clang.llvm.org/docs/LanguageExtensions.html

Here's a simple use case example. First the code:

void badFunction(void) __attribute__((unavailable("Don't use badFunction, it won't work.")));

int main(void)
{
    badFunction();
    return 0;
}

And then building it:

$ make example
cc     example.c   -o example
example.c:5:5: error: 'badFunction' is unavailable: Don't use badFunction, it
      won't work.
    badFunction();
    ^
example.c:1:6: note: function has been explicitly marked unavailable here
void badFunction(void) __attribute__((unavailable("Don't use...
     ^
1 error generated.
make: *** [example] Error 1
like image 171
Carl Norum Avatar answered Oct 04 '22 01:10

Carl Norum