OC调用传多值参数Swift方法的处理

截止目前, Swift 已经达到4.2版本。有很多优秀的Swift库。有时需要OC和Swift混编。例如 FaceAware 人脸感知开源库。

Swift方法传多值参数及OC调用

比如一个开源的图片处理框架,人脸感知库FaceAware
FaceAware GitHub 地址页面,在Swift 内部可以这样设置一个已知Image和Frame的方法,即多值参数。

    func setLocalFaceImageAndFrame(aImage: UIImage?, aFrame: CGRect?) {
        let layer = self.imageLayer()
        layer.contents = aImage!.cgImage
        layer.frame = aFrame!
    }

OC与Swift混编,在"ProjectName_iOS-Swift.h"的头文件中,并没有暴露出上述所写方法,若加上 @objc ,会报错

Method cannot be marked @objc because the type of the parameter 2 cannot be represented in Objective-C

不能运行。故调整如下:

把多值参数封装成一个Dictionary,通过Dictionary传值处理。

    @objc
    public func setLocalFaceImageAndFrame(infoDic: NSDictionary) {
        let aImage = infoDic["image"] as! UIImage
        let aFrame = infoDic["frame"]
        
        let layer = self.imageLayer()
        layer.contents = aImage.cgImage
        layer.frame = aFrame as! CGRect
    }

附加:
FaceAware https://github.com/BeauNouvelle/FaceAware

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐