我想过两种类型的实现; 在性能,可读性和可维护性方面,您认为哪一个更好?
像这样创建UIColor的扩展
extension UIColor {
class func myColor() -> UIColor {
return UIColor(red: 128/255, green: 102/255, blue: 0, alpha: 1)
}
}
Run Code Online (Sandbox Code Playgroud)创建一个结构:
struct Colors {
static let myColor = UIColor(red: 255/255, green: 102/255, blue: 0, alpha: 1)
}
Run Code Online (Sandbox Code Playgroud)我的代码中有自定义颜色.我多次使用它们,我想只分配一次.
如果我们看一下UIColor标题,我们可以看到以下内容:
[...]
// Some convenience methods to create colors. These colors will be as calibrated as possible.
// These colors are cached.
open class var black: UIColor { get } // 0.0 white
open class var darkGray: UIColor { get } // 0.333 white
[...]
Run Code Online (Sandbox Code Playgroud)
我创建了一个extensionUIColor,如下所示:
import UIKit
extension UIColor {
class func colorWithHexString(_ hex: String) -> UIColor {
print("\(#function): \(hex)")
// some code, then it return a UIColor
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> …Run Code Online (Sandbox Code Playgroud) 我的应用程序有一些自定义颜色,现在它像字典一样保存,但我认为这不是一个好主意,我想用自定义颜色对UIColor进行扩展.
这可能看起来像这样
var newColor = UIColor.MyColor // like UIColor.white
Run Code Online (Sandbox Code Playgroud)
也许我应该用扩展名添加我的颜色枚举?