iOS 13.0中不推荐使用'scanHexInt32'

时间:2019-09-10 12:10:33

标签: swift hex ios13 uicolor

iOS 13(Swift 5+)中scanHexInt32的替代形式是什么?

extension UIColor {


    //--------------------------------------------
    class func hexColor(hex:String) -> UIColor {
        var cString:String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()

        if (cString.hasPrefix("#")) {
            cString = String(cString[cString.index(cString.startIndex, offsetBy: 1)...])
        }

        if (cString.count != 6) {
            return UIColor.gray
        }

        var rgbValue:UInt32 = 0

// warning in this line - 'scanHexInt32' was deprecated in iOS 13.0
        Scanner(string: cString).scanHexInt32(&rgbValue)

        return UIColor(
            red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }
}

参考:快照

enter image description here

7 个答案:

答案 0 :(得分:7)

好像苹果正在从其64位操作系统中逐步淘汰Int32。尝试将您的代码转换为使用Int64。

Subject = " Blank plot with "cloglog" option to ggsurvplot"

Body = c("Please see the recent StackOverflow dialog on this issue.",
"https://stackoverflow.com/questions/57791504/in-r-survminerggsurvplot-with-fun-cloglog-returns-warning-message-and-emp/57793157#57793157",
"I suggest error trapping for the possibility of a zero value on the time axis.",
"Sincerely;")

答案 1 :(得分:6)

更新为使用deleted_atUInt64

scanHexInt64

答案 2 :(得分:4)

还有另一种实例方法可用

scanInt32(representation:)

声明:

func scanInt32(representation: Scanner.NumberRepresentation = .decimal) -> Int32?

在这里您必须传递枚举.hexadecimal

我希望它将返回相同的结果。结果将是可选的。

答案 3 :(得分:2)

尝试使用该扩展程序swiftui:

extension Color {
    init(hex: String) {
        let scanner = Scanner(string: hex)
        scanner.currentIndex = scanner.string.startIndex
        var rgbValue: UInt64 = 0
        scanner.scanHexInt64(&rgbValue)

        let r = (rgbValue & 0xff0000) >> 16
        let g = (rgbValue & 0xff00) >> 8
        let b = rgbValue & 0xff

        self.init(red: Double(r) / 0xff, green: Double(g) / 0xff, blue: Double(b) / 0xff)
    }
}

答案 4 :(得分:1)

雨燕5

extension String {

var color: UIColor {
    let hex = trimmingCharacters(in: CharacterSet.alphanumerics.inverted)

    if #available(iOS 13, *) {
        guard let int = Scanner(string: hex).scanInt32(representation: .hexadecimal) else { return #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) }

        let a, r, g, b: Int32
        switch hex.count {
        case 3:     (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)  // RGB (12-bit)
        case 6:     (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)                    // RGB (24-bit)
        case 8:     (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)       // ARGB (32-bit)
        default:    (a, r, g, b) = (255, 0, 0, 0)
        }

        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(a) / 255.0)

    } else {
        var int = UInt32()

        Scanner(string: hex).scanHexInt32(&int)
        let a, r, g, b: UInt32
        switch hex.count {
        case 3:     (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)  // RGB (12-bit)
        case 6:     (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)                    // RGB (24-bit)
        case 8:     (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)       // ARGB (32-bit)
        default:    (a, r, g, b) = (255, 0, 0, 0)
        }

        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(a) / 255.0)
    }
  }
}

答案 5 :(得分:1)

这结合了两个答案,同时提供了UInt64和String表示形式。它还删除了Color不需要的“便捷”关键字,因为它是一个结构(而UIColor因为它是一个类而需要)。最后,它使用Double而不是CGFloat-同样,对于Color来说是必需的。

void
mongo_dns_unresolved_push(char *host)
{
        bson_t *doc;
        bson_error_t error;
        bson_t *update = NULL;
        bson_t *query = NULL;
        doc = bson_new();
        BSON_APPEND_BOOL(doc, "upsert", "true");
        query = BCON_NEW("host", BCON_UTF8(host));
        update = BCON_NEW("$set", "{", "host", BCON_UTF8(host), "}");
        if (!mongoc_collection_update_one(unresolved_collection, query, update, doc, NULL, &error)) {
                fprintf (stderr, "%s\n", error.message);
        }
        bson_destroy(doc);
}

答案 6 :(得分:1)

我正在使用此uicolor扩展名..请找到以下代码

extension UIColor {

class func hexColor(hex:String) -> UIColor {
    var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

    if (cString.hasPrefix("#")) {
        cString.remove(at: cString.startIndex)
    }

    if ((cString.count) != 6) {
        return UIColor.gray
    }

    var rgbValue:UInt64 = 0
    Scanner(string: cString).scanHexInt64(&rgbValue)

    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
} }