There is a code:

func loadShots(shotsUrl: String, completion: (([Shot]) -> Void)!) { let urlString = "https://api.dribbble.com/v1/shots?access_token=" + accessToken let session = NSURLSession.sharedSession() let shotUrl = NSURL(string: urlString) let task = session.dataTaskWithURL(shotUrl!) { (data, response, error) -> Void in if error != nil { print(error?.localizedDescription) } else { var shots = [Shot]() var error : NSError? var shotsData = try? NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers ) as! NSArray for shot in shotsData! { let shot = Shot(data: shot as! NSDictionary) shots.append(shot) } let prioriry = DISPATCH_QUEUE_PRIORITY_DEFAULT dispatch_async(dispatch_get_global_queue(prioriry, 0)) { dispatch_async(dispatch_get_main_queue()) { completion(shots) } } } } task.resume() } 

Why shots after build shows 0 elements? What did I do wrong?

Closed due to the fact that it was off topic by participants aleksandr barakin , user207618, Streletz , Denis , user194374 Aug 31 '16 at 7:13 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - aleksandr barakin, Community Spirit, Streletz, Denis, Community Spirit
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Are you sure that something has come to you from the server? and where exactly are you trying to display the contents of the array (it is declared inside the block, so it does not live for a long time). And rename the shot just in case you have two of them there. - Max Mikheyenko 1:01 pm
  • and in order not to get up twice, do you have Shot(data:) exactly taking a Dictionary? Something seems to me that NSData would make more sense there - Max Mikheyenko
  • data comes from the server, I get 40000 bytes (if I hover on shotsData, or write "po shotsData" in the output console, and when I hoist on Shot, or po Shot writes 0 values, although in theory it should be filled. - Yurjke
  • as I said - rename one of the shot, and check that your init knows what to do with the Dictionary - Max Mikheyenko
  • init (data: NSDictionary) {self.id = data ["id"] as! Int let shots = data ["image"] as! NSDictionary self.imageUrl = getStringFromJSON (shots, key: "normal")} - Yurjke

1 answer 1

The problem was incorrectly described by init , class Shot