如何用json中的mutablearray填充我的tableview

所以我从一个json格式的url中获取数据。 我试图显示在我的tableview中的数据,但即使它感觉很简单,我不知道如何做到这一点。

class CompanyModel { func getJSON() { let companyArray: NSMutableArray = NSMutableArray() let requestURL: NSURL = NSURL(string: "http://localhost/Companies/JSON.php")! let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL) let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(urlRequest) { (data, response, error) -> Void in let httpResponse = response as! NSHTTPURLResponse let statusCode = httpResponse.statusCode if (statusCode == 200) { print("Everyone is fine, file downloaded successfully.") do{ let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) if let companies = json["companies"] as? [[String: AnyObject]] { for company in companies { if let name = company["name"] as? String, let phoneNumber = company["phone_number"] as? String, let website = company["website"] as? String, let email = company["email"] as? String, let address = company["address"] as? String { let company = CompanyModel() company.name = name company.phoneNumber = phoneNumber company.website = website company.email = email company.address = address } companyArray.addObject(company) print(companyArray) } } } catch { print("Error with Json: \(error)") } } print(companyArray) <- array is populated } print(companyArray) <- array is empty task.resume() } } 

我知道我以前做过….我猜viewDidLoad()我会打电话给CompanyModel().getJSON()这将获取数据,然后将其存储在一个空的数组,但我的大脑感觉空白怎么做。

我不能声明一个variables的NSarray,并直接存储它的variables的数据,然后填充tableview。 不过,我希望这解释了我想要达到的目标。

那么先改变函数返回你的公司数组:

 func getJSON() -> NSMutableArray { } 

在for循环的结尾返回公司数组

 for company in companies { } 

数组填充后,返回该块内的数组:

 dispatch_async(dispatch_get_main_queue(), { return companyArray }) 

task.resume()返回数组之后:

 return companyArray 

从任何地方你想调用这个类,并获得数组:

获得课程的参考

 Let companyModal = CompanyModel() 

而在任何你有你的表视图和类让我们说在viewDidLoad ,你应该首先有NSMutableArray

 var arraySource = NSMutableArray() 

viewDidLoad

 arraySource = companyModal.getJSON() 

并显示tableView中的数据做:

 Mytableview.reloadData() 

您不能在asynchronousnetworking请求的闭包中使用return ,而必须使用callback。

你需要一个来自请求的NSMutableArray,所以首先我们来callback一下:

 completion: (array: NSMutableArray)->() 

我们将这个callback添加到方法签名中:

 func getJSON(completion: (array: NSMutableArray)->()) 

然后在数组可用的位置 ,我们放置这个完成处理程序:

 class CompanyModel { func getJSON(completion: (array: NSMutableArray)->()) { let companyArray: NSMutableArray = NSMutableArray() let requestURL: NSURL = NSURL(string: "http://localhost/Companies/JSON.php")! let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL) let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(urlRequest) { (data, response, error) -> Void in let httpResponse = response as! NSHTTPURLResponse let statusCode = httpResponse.statusCode if (statusCode == 200) { do{ let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) if let companies = json["companies"] as? [[String: AnyObject]] { for company in companies { if let name = company["name"] as? String, let phoneNumber = company["phone_number"] as? String, let website = company["website"] as? String, let email = company["email"] as? String, let address = company["address"] as? String { let company = CompanyModel() company.name = name company.phoneNumber = phoneNumber company.website = website company.email = email company.address = address companyArray.addObject(company) } } // CALLBACK HERE completion(array: companyArray) } } catch { print("Error with Json: \(error)") } } } task.resume() } } 

现在从networking中获取数组,我们使用如下所示的尾部闭包:

 getJSON { (array) in print(array) }