It is necessary with the help of SwiftyJSON from VK json response to get some values, for example, id from the first items. (beginning of the sample answer from the official api VK)

response: { count: 617, items: [{ id: 67640, from_id: -86529522, owner_id: -86529522, date: 1465326080, post_type: 'post', text: 'Создатель [club41960539|Крошки Ши] на VK Fest' 

For this I try to use the following code.

 let urlPath: String = "https://api.vk.com/method/wall.get" let url: NSURL = NSURL(string: urlPath)! let request1: NSMutableURLRequest = NSMutableURLRequest(URL: url) request1.HTTPMethod = "POST" let stringPost="access_token=" + token // Key and Value let data = stringPost.dataUsingEncoding(NSUTF8StringEncoding) request1.timeoutInterval = 60 request1.HTTPBody=data request1.HTTPShouldHandleCookies=false let queue:NSOperationQueue = NSOperationQueue() NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in do { if let jsonResult = try JSON(data: data!) as? JSON { print("ASynchronous\(jsonResult)end") if let id = jsonResult["response"]["items"][0]["id"].string { print("\(id) ID") } 

But the line if let id = jsonResult ["response"] ["items"] [0] ["id"]. String is never true, i.e. where it appears nil. At the same time, I tried to output jsonResult to the terminal, it can be seen there

 { "response" : [ 655, { "id" : 830, "comments" : { "count" : 0, "can_post" : 1 }, "reply_count" : 0, "can_delete" : 1, 

A little bit is not what is described in the official api VKontakte. Where do I make a mistake?

  • I think that I have a mistake somewhere, some values ​​are lost in the process of converting the received data into json. By the way, I was able to get the id by contacting this if if let id = jsonResult ["response"] [1] ["id"]. RawString () {print ("(id) ID") I do not understand what the fundamental difference in the code). It seems to be a problem somewhere in my conversion process to json though. - George
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

If everything is correct and the answer comes as described in the official documentation , then it should be disassembled as follows:

 let id = ["response"][1]["id"].intValue 

not 0, because The first element is the number of entries (sort of).

.int returns an optional type and only if it is a / Bool number, otherwise nil . .intValue also works with String , if it cannot parse (if the Array/ Dictionary/null argument), then returns 0. For example, if the input is / Bool/String/Array and that returns:

 "id":219 .int: Optional(219) //Вот в этом случае можете if let... .intValue: 219 "id":true .int: Optional(1) .intValue: 1 "id":"219" .int: nil //не работает со String .intValue: 219 "id":[1, 219] .int: nil .intValue: 0 
  • Thank! That is how it worked. Instead of .int -> .intValue, instead of .string -> .stringValue. The only thing with this design, he refuses to work with if let. If it's not difficult for you, explain why in this case .intValue returns a value, but int does not? - George
  • @George updated the answer because did not fit into the comment - VAndrJ