gpt4 book ai didi

ios - '[字符串 ]' is not convertible to ' [NSString]'

转载 作者:行者123 更新时间:2023-11-29 01:38:52 24 4
gpt4 key购买 nike

我正在关注此链接上的教程: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
}
}

最佳答案

它使用以下语法:

var components = string.componentsSeparatedByString(",") as [NSString]

关于ios - '[字符串 ]' is not convertible to ' [NSString]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32651085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com