本文介绍了'String' 类型的值没有成员 'stringByTrimmingCharactersInSet'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将我的项目转换为 swift 3 后,我在此块的第一行收到以下 Value of type 'String' has no member 'stringByTrimmingCharactersInSet'
错误:
After converting my project to swift 3, I get the following Value of type 'String' has no member 'stringByTrimmingCharactersInSet'
error on the first line within this block:
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'
的语法变化引起的..对此有何更正?
I'm guessing the error derives from a change in syntax with 'stringByTrimmingCharactersInSet'
.. what is the correction for this?
推荐答案
新的语法是这样的:
var cString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
作为建议,您不需要指定 cString
是一个字符串,因为这是假定您分配给它的值.
As a suggestion, you don't need to specify that cString
is a String, as this is assumed with the value you are assigning to it.
这篇关于'String' 类型的值没有成员 'stringByTrimmingCharactersInSet'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!