我正在進行網絡調用並從API中檢索一些信息並將其存儲在NSMutableArray
中。在嘗試將該數組信息發送到另一個對象時,我碰壁了:
我沒有任何錯誤。我無法訪問另一個類中的數組信息。它在API類中打印效果很好,但當嘗試訪問另一個類時,數組會打印爲空。傳遞一個NSMutableArray到Swift中的另一個對象
這是我與一個NSMutableArray
在最高層API類來保存信息:
class API: NSObject {
var informationArray = NSMutableArray();
func getEarthquakeInformation() {
let session = NSURLSession.sharedSession()
let urlString = "http://ehp2-earthquake.wr.usgs.gov/fdsnws/event/1/query?format=geojson&limit=20"
let url = NSURL(string: urlString)!
let request = NSURLRequest(URL: url)
let task = session.dataTaskWithRequest(request){ data,response,downloadError in
if let error = downloadError {
print("could not complete the request\(error)")
} else {
let parsedResult = try! NSJSONSerialization.JSONObjectWithData(data! , options: NSJSONReadingOptions.AllowFragments)
let dataDict = parsedResult as! NSDictionary
// This holds all the information we need from the API.
if let result = dataDict["features"] {
for var i = 0; i < result.count; i++ {
self.informationArray.addObject(result[i])
print(self.informationArray[i])
}
} else {
print("error")
}
//print("info from the array \(self.informationArray)")
}
}
task.resume()
}
}
這是我試圖在發送到類:的MapViewController
您的來電(NSURLSession東西)是異步。在「print(self.informationArray [i])」之前打印「print(apiObject.informationArray)」。使用塊(關閉我認爲在Swift中) – Larme
問題是什麼?你打的牆是什麼? – Cristik
什麼是錯誤或打印輸出? – Ismail