objective-c - Objective C 中可选的参数化 swift 框架方法?

标签 objective-c swift cocoa-touch

我有一个用 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/

相关文章:

ios - 使用 UICollectionViewFlowLayout/UICollectionViewLayout 子类,不会在正确的时间调用 CellForItemAtIndexPath

ios - 在 iOS 设备中保存从 Web 下载的图像的最佳位置?

ios - 将照片库中的图像复制到另一个目录

ios - 仅触摸 Swift3 中 SKSpriteNode 的非透明像素

Swift float 乘法错误

iphone - 我的自定义类中未调用 Touches Began 方法

iphone - ALAssetsLibrary 获取视频路径稍后播放

ios - 在 swift、iOS playground 中读取文件

iphone - 如何获取 NSURLConnection 中的默认用户代理字符串?

ios - 为什么在使用 TextKit 呈现文本时第二行最终会比第一行短?