Swift:类型(_)的值 - >没有成员'生成器'

时间:2016-02-16 23:30:09

标签: xcode swift

我有这段代码从Swift 2.0中的十六进制代码生成RGB值

    class func colorWithHex(hexString: String?, alpha: CGFloat) -> UIColor? {

    if let hexString = hexString {

        var error : NSError? = nil

        let regexp = try! NSRegularExpression(pattern:"\\A#[0-9a-f]{6}\\z", options: [.CaseInsensitive])

        let thrange = NSMakeRange(0, count(hexString))

        let thcount = regexp.numberOfMatchesInString(hexString, options: .ReportProgress, range: thrange)

        if thcount != 1 {

            return nil
        }

        var rgbValue : UInt32 = 0

        let scanner = NSScanner(string: hexString)

        scanner.scanLocation = 1
        scanner.scanHexInt(&rgbValue)

        let red   = CGFloat( (rgbValue & 0xFF0000) >> 16) / 255.0
        let green = CGFloat( (rgbValue & 0xFF00) >> 8) / 255.0
        let blue  = CGFloat( (rgbValue & 0xFF) ) / 255.0

        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
但是,它会抛出一个错误,我想这可能是因为代码是为Swift 1.2编写的,但我还不太了解Swift语言,但还没有解析这个错误试图告诉的内容我

let thrange = NSMakeRange(0, count(hexString))count(hexString)引发错误value of type '(_)-> _'has no member 'Generator'这是什么意思?

1 个答案:

答案 0 :(得分:2)

在Swift 2.0中count()被移除,现在您使用hexString.characters.count