我正在进行网络呼叫并从API检索一些信息并将其存储在NSMutableArray
中。当我试图将数组信息发送到另一个对象时,我碰壁了:
我没有任何错误。我无法访问另一个类中的数组信息。它在API类中打印得很好但在尝试访问另一个类时,数组打印为空。
这是我的API类,最顶层有NSMutableArray
来保存信息:
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
override func viewDidLoad() {
super.viewDidLoad()
// Instance of the api class
let apiObject = API()
apiObject.getEarthquakeInformation()
print(apiObject.informationArray)
答案 0 :(得分:1)
这里有几件事:
首先,请查看使用网络库(例如AlamoFire)
第二次,当你声明你的可变数组时,你应该以这种形式完成它(没有理由使用NSMutableArray()):
var myArray = [ObjectType]()
第三,不要使用C风格的for循环,它们被标记为从Swift中删除。你应该迭代:
for item in result {
//Do something with item.
}
,只要将数组“发送”到MapViewController对象即可。 如果API对象存在于MapViewController中,那么您可以将API函数作为参数接受闭包。您可以将数组传递回闭包本身的MapViewController。 或者您也可以使用通知。
希望这有帮助。
答案 1 :(得分:0)
let apiObject = API()
// Your problem it here this method takes time to update informationArray
apiObject.getEarthquakeInformation()
// But this line print immediately while it's still empty.
print(apiObject.informationArray)
你做的是
informationArray
课程中删除API
并将其放入MapViewController
更改getEarthquakeInformation
方法以获得竞争处理程序
class API: NSObject {
// remove it //var informationArray = NSMutableArray();
func getEarthquakeInformation(compeletion: (informationArray: [AnyObject]!) ->()) {
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"] {
compeletion(result)
} else {
print("error")
}
}
}
task.resume()
}
然后在MapViewController
var informationArray: [AnyObject]!
override func viewDidLoad() {
super.viewDidLoad()
// Instance of the api class
let apiObject = API()
apiObject.getEarthquakeInformation() { [unowned self] (result) ->() in
self.inforamtionArray = result
print(self.informationArray)
}