0
我下面這個鏈接教程:http://www.raywenderlich.com/97014/use-cocoapods-with-swift「[字符串]」不是轉換爲「[的NSString]」
據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
}
}
我很驚訝,因爲他的代碼是爲我工作的罰款。 –
OP的代碼在Swift 1.2中很好,錯誤是針對Swift 2的(但實際上OP沒有給出這個信息,我會在我能的時候給他們的問題添加swift2標籤(因爲現在有一個未決的建議編輯) )。 – Moritz
感謝您的信息好友.. :) –