gpt4 book ai didi

ios - 类型 'String' 的值没有成员 'stringByTrimmingCharactersInSet'

转载 作者:IT王子 更新时间:2023-10-29 05:10:08 25 4
gpt4 key购买 nike

将我的项目转换为 swift 3 后,我在该 block 的第一行收到以下 Value of type 'String' has no member 'stringByTrimmingCharactersInSet' 错误:

extension UIColor {
convenience init (hex:String) {
var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercased() // error appears on this line

if (cString.hasPrefix("#")) {
cString = (cString as NSString).substring(from: 1)
}



let rString = (cString as NSString).substring(to: 2)
let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)

var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
Scanner(string: rString).scanHexInt32(&r)
Scanner(string: gString).scanHexInt32(&g)
Scanner(string: bString).scanHexInt32(&b)


self.init(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
}
}

我猜这个错误是由于 'stringByTrimmingCharactersInSet' 的语法变化引起的。对此有何更正?

最佳答案

新的语法是这样的:

var cString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()

作为一个建议,您不需要指定 cString 是一个字符串,因为这是假定您分配给它的值。

关于ios - 类型 'String' 的值没有成员 'stringByTrimmingCharactersInSet',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38644568/

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