我的代码中有自定义颜色.我多次使用它们,我想只分配一次.
如果我们看一下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)