我正在关注此链接上的教程:http://www.raywenderlich.com/97014/use-cocoapods-with-swift
根据Xcode,行上有错误
var components = (string as NSString).componentsSeparatedByString(",") as! [NSString]
它说'[String]'不能转换为'[NSString]'。究竟发生了什么,我该如何解决?
谢谢!这是代码:
import UIKit
func RGB(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
return RGBA(red, green: green, blue: blue, alpha: 255)
}
func RGBA(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) -> UIColor {
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: alpha/255)
}
extension UIColor {
class func RGBAColorFromString(string: String?) -> UIColor? {
if let string = string {
var components = (string as NSString).componentsSeparatedByString(",") as! [NSString]
if components.count == 3 {
components.append("1.0")
}
if components.count != 4 {
return nil
}
let red = CGFloat(components[0].floatValue / 255)
let green = CGFloat(components[1].floatValue / 255)
let blue = CGFloat(components[2].floatValue / 255)
let alpha = CGFloat(components[3].floatValue / 255)
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
return nil
}
}
答案 0 :(得分:1)
它适用于这种语法:
var components = string.componentsSeparatedByString(",") as [NSString]