-2

Need help with swift 3. I got

cannot convert value of type string to expected argument type string

 func authKMA(text: String) {

     let base = "http://api.kma1.biz/?method=auth"
     let userlogin = "&username=vostok3r@gmail.com"
     let userpass = "&pass=0000"
     _ = ""
     _ = ""
     let auth = base + userlogin + userpass

     let url = URL(string: auth)!

     URLSession.shared.dataTask(with: url) { (data, _, _) in
        guard let gotdata = data else {
            print("Сервер не отвечает")
            return
        }

        guard  let jsonAny = try? JSONSerialization.jsonObject(with: gotdata, options: []) else {
            print("Сервер не дал информацию по аккаунту")
            return
        }

        guard let json = jsonAny  as? [String: Any] else {
            return
        }

        guard let authhasher = json.index(of: "authhash") else {
            return
   //ERROR: cannot convert value of type string to expected argument type string//  
        }    
        print(authhasher)
    }.resume()
}
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Leon Kazakov
  • 7
  • 1
  • 4
  • It seems like a weird error, but `Dictionary` doesn't have a method `index(of:)`, so that line shouldn't work. What are you trying to achieve using `let authhasher = json.index(of: "authhash")`? – Dávid Pásztor Jul 24 '17 at 17:00
  • @DávidPásztor im just a newbee and trying to get "authhash" value from json array. – Leon Kazakov Jul 24 '17 at 17:04
  • Possible duplicate of [Correctly Parsing JSON in Swift 3](https://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3) – Nazmul Hasan Jul 24 '17 at 17:07
  • @LeonKazakov that is a Dictionary, not an Array. You can access "authhash" as toddg mentioned in his answer. – Dávid Pásztor Jul 24 '17 at 18:16

1 Answers1

1

As Dávid Pásztor commented, Dictionary doesn't have index(of:) method. If all you're trying to do is extract the authhash value from your json dictionary, you can access your values using subscript notation:

guard let authhasher = json["authhash"] as? String else {
        return  
}
print(authhasher)    
toddg
  • 2,863
  • 2
  • 18
  • 33
  • Great! Glad it worked for you. Tap the check mark to accept this answer and indicate to future viewers that it solved your problem. – toddg Jul 24 '17 at 19:05