gpt4 book ai didi

swift - 'scanHexInt32' 在 iOS 13.0 中被弃用

转载 作者:行者123 更新时间:2023-12-04 01:46:12 30 4
gpt4 key购买 nike

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

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

最佳答案

更新以使用 UInt64scanHexInt64 :

convenience init(hex: String, alpha: CGFloat = 1.0) {
var hexFormatted: String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()

if hexFormatted.hasPrefix("#") {
hexFormatted = String(hexFormatted.dropFirst())
}

assert(hexFormatted.count == 6, "Invalid hex code used.")

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

self.init(red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: alpha)
}

关于swift - 'scanHexInt32' 在 iOS 13.0 中被弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57870527/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com