NSInvocation equivalent

Hello Swift Users,

  This is a Swift3 newbie question that I’ve not found a solution to. Has there been any consensus reached on an equivalent approach to the AppKit class NSInvocation ? Here’s a simple objC example.

-(NSInteger)totalCount
{
    NSInteger totalCount = -1;
    if ([self.representedObject respondsToSelector:@selector(totalCount)])
    {
       SEL selector = @selector(totalCount);
        NSMethodSignature *aSignature = [[self.representedObject class] instanceMethodSignatureForSelector:selector];

        if (aSignature != nil)
        {
            NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature:aSignature];
            [anInvocation setSelector:selector];
            [anInvocation setTarget:self.representedObject];
            [anInvocation invoke];
            [anInvocation getReturnValue:&totalCount];
        }
    }
    return totalCount;
}

Thanks and regards,
RFM

Swift doesn’t have an equivalent because the language simply isn’t as dynamic as Objective-C: there is no mechanism to invoke a method at runtime given its name.

If you’re on an Apple platform, and the method you want to call, and its class, are @objc — then you can literally use NSInvocation from Swift to do this. But not otherwise.

—Jens

···

On Oct 5, 2016, at 1:50 PM, Bob Miller via swift-users <swift-users@swift.org> wrote:

  This is a Swift3 newbie question that I’ve not found a solution to. Has there been any consensus reached on an equivalent approach to the AppKit class NSInvocation ? Here’s a simple objC example.

However, there are usually better answers than NSInvocation (both in Swift and Objective-C). What are you actually trying to do?

Jordan

···

On Oct 5, 2016, at 14:43, Jens Alfke via swift-users <swift-users@swift.org> wrote:

On Oct 5, 2016, at 1:50 PM, Bob Miller via swift-users <swift-users@swift.org> wrote:

  This is a Swift3 newbie question that I’ve not found a solution to. Has there been any consensus reached on an equivalent approach to the AppKit class NSInvocation ? Here’s a simple objC example.

Swift doesn’t have an equivalent because the language simply isn’t as dynamic as Objective-C: there is no mechanism to invoke a method at runtime given its name.

If you’re on an Apple platform, and the method you want to call, and its class, are @objc — then you can literally use NSInvocation from Swift to do this. But not otherwise.

Yeah, the only time I’d recommend using NSInvocation is when implementing a proxy object using -forwardInvocation:, but that's explicitly disallowed in Swift. In other situations invocations are generally too slow, awkward to use, and hard to debug.

—Jens

···

On Oct 5, 2016, at 4:24 PM, Jordan Rose <jordan_rose@apple.com> wrote:

However, there are usually better answers than NSInvocation (both in Swift and Objective-C). What are you actually trying to do?

If you could define your needs. Is it a one to one ? Or one to many
relationship. I would look into the Observer Design pattern.

···

On Oct 5, 2016 7:26 PM, "Jordan Rose via swift-users" <swift-users@swift.org> wrote:

> On Oct 5, 2016, at 14:43, Jens Alfke via swift-users < > swift-users@swift.org> wrote:
>
>
>> On Oct 5, 2016, at 1:50 PM, Bob Miller via swift-users < > swift-users@swift.org> wrote:
>>
>> This is a Swift3 newbie question that I’ve not found a solution
to. Has there been any consensus reached on an equivalent approach to the
AppKit class NSInvocation ? Here’s a simple objC example.
>
> Swift doesn’t have an equivalent because the language simply isn’t as
dynamic as Objective-C: there is no mechanism to invoke a method at runtime
given its name.
>
> If you’re on an Apple platform, and the method you want to call, and its
class, are @objc — then you can literally use NSInvocation from Swift to do
this. But not otherwise.

However, there are usually better answers than NSInvocation (both in Swift
and Objective-C). What are you actually trying to do?

Jordan

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users