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

  • Post author:
  • Post category:其他




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



版权声明:本文为u012709932原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。