超载' class func'在迅速

时间:2017-08-02 20:58:40

标签: swift class func

我正在使用一个小图书馆创建一个带有某人名字缩写的图片。我有一个实际有效的类和一个类func class func imageWith(name: String?, textColor: UIColor = .white) -> UIImage?我之前从未使用class func,我通常会使用static我想知道它是否可能用class func imageWith(name: [String]?, textColor: UIColor = .white) -> UIImage?

重载函数

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

您获得的错误消息是由于在Swift 3中,Objective-C运行时隐式可用方法,而Objective-C不支持重载。有两种方法可以解决这个问题:

  1. 升级到Swift 4,删除隐式的@objc推理。
  2. 或:

    1. 将@nonobjc添加到一个或两个func声明中。

答案 1 :(得分:0)

这是一个Objective-C转换问题。

Objective-C在调用方法时没有对类强制执行类型区分,因此您不能使用具有相同名称的方法。

Swift看到的地方

class func imageWith(name: String?, textColor: UIColor = .white) -> UIImage?

class func imageWith(name: [String]?, textColor: UIColor = .white) -> UIImage?

Objective-C看

+(id)imageWithName: (id) name andTextColor: (id) textColor

+(id)imageWithName: (id) name andTextColor: (id) textColor

<强>解决方案:

你需要做的就是改变你的命名,.e.g。

class func imageWith(names: [String]?, textColor: UIColor = .white) -> UIImage?

将导致Objective-C看到

+(id)imageWithNames: (id) names andTextColor: (id) textColor