IOS9 - 无法使用“(String)”类型的参数列表调用 'count'
IOS9 - cannot invoke 'count' with an argument list of type '(String)'
我刚迁移到 Xcode7/IOS9,我的部分代码不兼容。
我从 Xcode 得到以下错误:
" 无法使用 '(String)' 类型的参数列表调用 'count' "
这是我的代码:
let index = rgba.startIndex.advancedBy(1)
let hex = rgba.substringFromIndex(index)
let scanner = NSScanner(string: hex)
var hexValue: CUnsignedLongLong = 0
if scanner.scanHexLongLong(&hexValue)
{
if count(hex) == 6
{
red = CGFloat((hexValue & 0xFF0000) >> 16) / 255.0
green = CGFloat((hexValue & 0x00FF00) >> 8) / 255.0
blue = CGFloat(hexValue & 0x0000FF) / 255.0
}
else if count(hex) == 8
{
red = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0
green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
blue = CGFloat((hexValue & 0x0000FF00) >> 8) / 255.0
alpha = CGFloat(hexValue & 0x000000FF) / 255.0
}
在 swift2 他们对 count
做了一些改动
这是 swift 1.2:
的代码
let test1 = "ajklsdlka"//random string
let length = count(test1)//character counting
因为 swift2 代码必须是
let test1 = "ajklsdlka"//random string
let length = test1.characters.count//character counting
为了能够求出数组的长度
此行为主要发生在 swift 2.0 中,因为 String
不再符合 SequenceType
协议,而 String.CharacterView
符合
请记住,它还改变了您在数组中迭代的方式:
var password = "Meet me in St. Louis"
for character in password.characters {
if character == "e" {
print("found an e!")
} else {
}
}
所以要非常小心,尽管很可能 Xcode 会给你这样的操作一个错误。
为了修复您遇到的错误,您的代码应该是这样的(无法使用“(String)”类型的参数列表调用 'count'):
let index = rgba.startIndex.advancedBy(1)
let hex = rgba.substringFromIndex(index)
let scanner = NSScanner(string: hex)
var hexValue: CUnsignedLongLong = 0
if scanner.scanHexLongLong(&hexValue)
{
if hex.characters.count == 6 //notice the change here
{
red = CGFloat((hexValue & 0xFF0000) >> 16) / 255.0
green = CGFloat((hexValue & 0x00FF00) >> 8) / 255.0
blue = CGFloat(hexValue & 0x0000FF) / 255.0
}
else if hex.characters.count == 8 //and here
{
red = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0
green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
blue = CGFloat((hexValue & 0x0000FF00) >> 8) / 255.0
alpha = CGFloat(hexValue & 0x000000FF) / 255.0
}
我刚迁移到 Xcode7/IOS9,我的部分代码不兼容。
我从 Xcode 得到以下错误:
" 无法使用 '(String)' 类型的参数列表调用 'count' "
这是我的代码:
let index = rgba.startIndex.advancedBy(1)
let hex = rgba.substringFromIndex(index)
let scanner = NSScanner(string: hex)
var hexValue: CUnsignedLongLong = 0
if scanner.scanHexLongLong(&hexValue)
{
if count(hex) == 6
{
red = CGFloat((hexValue & 0xFF0000) >> 16) / 255.0
green = CGFloat((hexValue & 0x00FF00) >> 8) / 255.0
blue = CGFloat(hexValue & 0x0000FF) / 255.0
}
else if count(hex) == 8
{
red = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0
green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
blue = CGFloat((hexValue & 0x0000FF00) >> 8) / 255.0
alpha = CGFloat(hexValue & 0x000000FF) / 255.0
}
在 swift2 他们对 count
这是 swift 1.2:
的代码let test1 = "ajklsdlka"//random string
let length = count(test1)//character counting
因为 swift2 代码必须是
let test1 = "ajklsdlka"//random string
let length = test1.characters.count//character counting
为了能够求出数组的长度
此行为主要发生在 swift 2.0 中,因为 String
不再符合 SequenceType
协议,而 String.CharacterView
符合
请记住,它还改变了您在数组中迭代的方式:
var password = "Meet me in St. Louis"
for character in password.characters {
if character == "e" {
print("found an e!")
} else {
}
}
所以要非常小心,尽管很可能 Xcode 会给你这样的操作一个错误。
为了修复您遇到的错误,您的代码应该是这样的(无法使用“(String)”类型的参数列表调用 'count'):
let index = rgba.startIndex.advancedBy(1)
let hex = rgba.substringFromIndex(index)
let scanner = NSScanner(string: hex)
var hexValue: CUnsignedLongLong = 0
if scanner.scanHexLongLong(&hexValue)
{
if hex.characters.count == 6 //notice the change here
{
red = CGFloat((hexValue & 0xFF0000) >> 16) / 255.0
green = CGFloat((hexValue & 0x00FF00) >> 8) / 255.0
blue = CGFloat(hexValue & 0x0000FF) / 255.0
}
else if hex.characters.count == 8 //and here
{
red = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0
green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
blue = CGFloat((hexValue & 0x0000FF00) >> 8) / 255.0
alpha = CGFloat(hexValue & 0x000000FF) / 255.0
}