I am looking to migrate some code from Swift -> Objective C starting with unit tests.
I am having issues with mocking functions that passes value types:
let initWithTitleBlock : @objc_block (String, String) -> AlertViewMock! = { (title : String, message : String) -> (AlertViewMock!) in
alert = AlertViewMock(title: title, message: message)
return alert
}
let initWithTitleImp = imp_implementationWithBlock(unsafeBitCast(initWithTitleBlock, AnyObject.self))
let initWithTitleMethod = class_getInstanceMethod(AlertView.self, Selector("initWithTitle:message:"))
method_setImplementation(initWithTitleMethod, initWithTitleImp)
But I end up getting the following error at runtime:
-[AlertView copyWithZone:]: unrecognized selector sent to instance 0x7fef006e4cb0
However, if I change the type getting passed into the block as AnyObject
or some other reference type like NSURL
, I am able to successfully run the new block (except I cannot convert the object back into a string):
let initWithTitleBlock : @objc_block (AnyObject, AnyObject) -> AlertViewMock! = { (title : AnyObject, message : AnyObject) -> (AlertViewMock!) in ...
I did not mark AlertViewMock
as copy
. May anyone help explain what's going on? If I pass in NSInteger
, I get the same exception:
let initWithTitleBlock : @objc_block (NSInteger, NSInteger) -> AlertViewMock! = { (title : NSInteger, message : NSInteger) -> (AlertViewMock!) in ...