全局变量不包含任何数据。 在范围内有数据。NSLog告诉我,我在函数GetJsonDataFromURL中的数据包含数据。
'请向我解释为什么以及处理变量和数据的行业标准方法是什么,现在已经调试了几天了"
var employeeData : Dictionary<String, AnyObject> = [:]
override func viewDidLoad() {
super.viewDidLoad()
GetJsonDataFromURL()
NSLog("%@", employeeData) <- THIS LINE HAS NO DATA
}
func GetJsonDataFromURL() {
let postEndpoint: String = "http://localhost/watsdis/showOutbox.php"
let session = NSURLSession.sharedSession()
let url = NSURL(string: postEndpoint)!
var dataContainer : Dictionary <String, String> = [:]
var mobiles : Dictionary <String, String> = [:]
var msgs : Dictionary <String, String> = [:]
var ids : [String] = []
var names : Dictionary <String, String> = [:]
var pos = ""
// Make the POST call and handle it in a completion handler
session.dataTaskWithURL(url, completionHandler: { ( data: NSData?, response: NSURLResponse?, let error: NSError?) -> Void in
// Make sure we get an OK response
guard let realResponse = response as? NSHTTPURLResponse where
realResponse.statusCode == 200 else {
print("Not a 200 response")
return
}
// Read the JSON
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
if let employees = json["employee"] as? [[String: AnyObject]] {
for employee in employees {
if let id = employee["id"] as? String {
ids.append (id)
pos = id
}
if let mobile = employee["mobile"] as? String {
mobiles[pos] = mobile
}
if let msg = employee["msg"] as? String {
msgs[pos] = msg
}
if let name = employee["name"] as? String {
names[pos] = name
}
}
}
self.employeeData["id"] = ids
self.employeeData["name"] = names
self.employeeData["mobile"] = mobiles
self.employeeData["msg"] = msgs
} catch {
print("error serializing JSON: (error)")
}
NSLog("%@", self.employeeData) <- THIS LINE DISPLAYS MY DATA IN ARRAY FORMAT
}).resume();
}
GetJsonDataFromURL
是异步函数在viewDidLoad
中,您调用:
GetJsonDataFromURL()
NSLog("%@", employeeData) <- THIS LINE HAS NO DATA
NSLog 在函数 GetJsonDataFromURL 返回数据之前运行,因此它是空数据。你试试:
func GetJsonDataFromURL(handleComplete:(isOK:Bool)->()) {
let postEndpoint: String = "http://localhost/watsdis/showOutbox.php"
let session = NSURLSession.sharedSession()
let url = NSURL(string: postEndpoint)!
var dataContainer : Dictionary <String, String> = [:]
var mobiles : Dictionary <String, String> = [:]
var msgs : Dictionary <String, String> = [:]
var ids : [String] = []
var names : Dictionary <String, String> = [:]
var pos = ""
// Make the POST call and handle it in a completion handler
session.dataTaskWithURL(url, completionHandler: { ( data: NSData?, response: NSURLResponse?, let error: NSError?) -> Void in
// Make sure we get an OK response
guard let realResponse = response as? NSHTTPURLResponse where
realResponse.statusCode == 200 else {
print("Not a 200 response")
return
}
// Read the JSON
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
if let employees = json["employee"] as? [[String: AnyObject]] {
for employee in employees {
if let id = employee["id"] as? String {
ids.append (id)
pos = id
}
if let mobile = employee["mobile"] as? String {
mobiles[pos] = mobile
}
if let msg = employee["msg"] as? String {
msgs[pos] = msg
}
if let name = employee["name"] as? String {
names[pos] = name
}
}
}
self.employeeData["id"] = ids
self.employeeData["name"] = names
self.employeeData["mobile"] = mobiles
self.employeeData["msg"] = msgs
handleComplete(isOK: true)
} catch {
handleComplete(isOK: false)
print("error serializing JSON: (error)")
}
NSLog("%@", self.employeeData) <- THIS LINE DISPLAYS MY DATA IN ARRAY FORMAT
}).resume();
}
并在viewDidLoad
self.GetJsonDataFromURL { (isOK) -> () in
if isOK{
print(self.employeeData)
}else{
print("error")
}
}
您正在使用回调调用异步方法。很有可能在执行将数据放入 employeeData 的回调之前检查变量。