Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS9 - cannot invoke 'count' with an argument list of type '(String)'

I just migrate to Xcode7/IOS9 and some part of my code are not compatible.

i get the following error from Xcode :

" cannot invoke 'count' with an argument list of type '(String)' "

This is my code :

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
    }
like image 521
f1rstsurf Avatar asked Sep 23 '15 13:09

f1rstsurf


1 Answers

In swift2 they did some changes on count

this is the code for swift 1.2:

let test1 = "ajklsdlka"//random string
let length = count(test1)//character counting

since swift2 the code would have to be

let test1 = "ajklsdlka"//random string
let length = test1.characters.count//character counting

In order to be able to find the length of an array.

This behaviour mainly happens in swift 2.0 because String no longer conforms to SequenceType protocol while String.CharacterView does

Keep in mind that it also changed the way you are iterating in an array:

var password = "Meet me in St. Louis"
for character in password.characters {
    if character == "e" {
        print("found an e!")
    } else {
    }
}

So be really careful although most likely Xcode is going to give you an error for operations like these one.

So this is how your code should look in order to fix that error you are having (cannot invoke 'count' with an argument list of type '(String)'):

  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
    }
like image 138
Korpel Avatar answered Dec 18 '22 19:12

Korpel