Skip to content

piotrtobolski/SwiftyInvocation

Repository files navigation

SwiftyInvocation

CI Status CocoaPods compatible Carthage compatible Twitter Version License Platform

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.

Example

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")

Requirements

Swift 3, Objective-C runtime

Installation

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

Author

Piotr Tobolski, piotr.tobolski@me.com

License

SwiftyInvocation is available under the MIT license. See the LICENSE file for more info.