Search code examples
cocoamacosretainnsmenuitem

NSMenuItem -setTarget: Does it retain the target?


NSMenuItem -setTarget: Does it retain the target, or should one explicitly retain it?

I've seen conflicting docs on this. I know of retainArguments in NSInvocation, but I'm not sure this applies to NSMenuItem as it doesn't inherit from NSInvocation.


Solution

  • Simply look at the header:

    @property (nullable, weak) id target;
    

    or in Swift:

    weak var target: AnyObject? { get set }
    

    Here we see that target is weak, meaning that it doesn't have control over the target's lifetime. In technical terms, it neither increments nor decrements the reference count. Once your target is deallocated for any reason, NSMenuItem loses it too.