ios – 快速扩展(类别)中的类功能

前端之家收集整理的这篇文章主要介绍了ios – 快速扩展(类别)中的类功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
可以在 swift的扩展中定义一个类函数,就像在一个Objective-C类中,你还可以定义类函数

目标c中的示例

  1. @implementation UIColor (Additions)
  2.  
  3. + (UIColor)colorWithHexString:(NSString *)hexString
  4. {
  5. // create color from string
  6. // ... some code
  7. return newColor;
  8. }
  9.  
  10. @end

什么是相当于迅速?

解决方法

是的,它可能和非常相似,主要区别是Swift扩展名没有命名.
  1. extension UIColor {
  2. class func colorWithHexString(hexString: String) -> UIColor {
  3. // create color from string
  4. // ... some code
  5. return newColor
  6. }
  7. }

猜你在找的iOS相关文章