我有一个用 swift 开发的框架,我在我的 Objective-C 代码库中使用该框架。
在框架中我有以下类和方法
public class MyClass: NSObject {
public func testMethod(typeId : Int?){
}
}
当我将 typeId Int? 可选更改为 Int 时,我可以在 Objective C 中使用 Int 访问该 testMethod ? 可选参数 我无法在 Objective C 代码中访问该 testMethod 。知道如何在 Objective C 中访问可选的 swift 参数化方法吗?
最佳答案
尝试像这样重写你的方法:
public class MyClass: NSObject {
//this method can be called only from swift and can recieve nil
public func testMethod(typeId : Int?){
}
//this method can be called from swift and objective c but both can´t recieve nil instead you can send 0 or another value and check it
public func testMethod(typeId : Int){ // override method
testMethod(typeId:typeId == 0 ? nil : ticketId) //original method
}
}
关于objective-c - Objective C 中可选的参数化 swift 框架方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35548321/