View source code
Display the source code in core/attribute.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Enum core.attribute.optional

Use this attribute to make an Objective-C interface method optional.

enum optional : void { ... }

An optional method is a method that does **not** have to be implemented in the class that implements the interface. To safely call an optional method, a runtime check should be performed to make sure the receiver implements the method.

This is a special compiler recognized attribute, it has several requirements, which all will be enforced by the compiler:

* The attribute can only be attached to methods which have Objective-C linkage. That is, a method inside an interface declared as extern (Objective-C)

* It can only be used for methods that are declared inside an interface * It can only be used once in a method declaration * It cannot be attached to a method that is a template

Enum members

NameDescription

Examples

import core.attribute : optional, selector;

extern (Objective-C):

struct objc_selector;
alias SEL = objc_selector*;

SEL sel_registerName(in char* str);

extern class NSObject
{
    bool respondsToSelector(SEL sel) @selector("respondsToSelector:");
}

interface Foo
{
    @optional void foo() @selector("foo");
    @optional void bar() @selector("bar");
}

class Bar : NSObject
{
    static Bar alloc() @selector("alloc");
    Bar init() @selector("init");

    void bar() @selector("bar")
    {
    }
}

extern (D) void main()
{
    auto bar = Bar.alloc.init;

    if (bar.respondsToSelector(sel_registerName("bar")))
        bar.bar();
}

Authors

Jacob Carlborg

License

Boost License 1.0