Have you ever dreamed about using NSInvocation from Swift code? You hate Apple for the NS_SWIFT_UNAVAILABLE macro?
Me neither.
But sometimes we just HAVE to use it. And here it is.
Check the unit tests.
let array: NSArray = ["1", "2", "3"]
let selector = #selector(NSArray.object(at:))
let methodSignature = NSArray.si_instanceMethodSignature(for: selector)
let invocation = SIInvocation(methodSignature: methodSignature)
invocation.target = array
invocation.selector = selector
var argument = 1
invocation.setArgument(&argument, at: 2)
invocation.retainArguments()
invocation.invoke()
var returnValue: Unmanaged<NSString>?
invocation.getReturnValue(&returnValue)
XCTAssertEqual(returnValue?.takeUnretainedValue(), "2")
Swift 3, Objective-C runtime
SwiftyInvocation is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SwiftyInvocation"
It is also available through Carthage:
github "piotr-tobolski/SwiftyInvocation" ~> 0.1.0
Piotr Tobolski, piotr.tobolski@me.com
SwiftyInvocation is available under the MIT license. See the LICENSE file for more info.