Tag: swift protocols

如何在Swift中使用协议公开一个Objective-C对象的私有类方法?

考虑UIColor上的两个私有方法: 实例方法styleString ,它返回颜色的RGBstring 类方法_systemDestructiveTintColor ,它返回破坏性button使用的红色。 UIColor.h私人头文件供参考 例如方法,我可以创build一个@objc协议,并使用unsafeBitCast公开私有方法: @objc protocol UIColorPrivate { func styleString() -> UIColor } let white = UIColor.whiteColor() let whitePrivate = unsafeBitCast(white, UIColorPrivate.self) whitePrivate.styleString() // rgb(255,255,255) 但是,我不知道这是如何工作的类方法。 第一次尝试: @objc protocol UIColorPrivate { class func _systemDestructiveTintColor() -> String // Error: Class methods are only allowed within classes } 有道理,我会把它改成static : @objc protocol UIColorPrivate { […]