ios – 类型’String’的值没有成员’stringByTrimmingCharactersInSet’

前端之家收集整理的这篇文章主要介绍了ios – 类型’String’的值没有成员’stringByTrimmingCharactersInSet’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
将我的项目转换为 swift 3后,我得到以下值“String”,该块中的第一行没有成员“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是一个字符串,因为这是假定与您分配给它的值.

原文链接:https://www.f2er.com/iOS/329347.html

猜你在找的iOS相关文章