我已經從API解析了我的數據,但是當我嘗試從解析的NSDictionary數據中設置一些值時,println語句不會被調用。它發生在字典中用「用戶」鍵,我嘗試設置學生的名字和姓氏。JSON解析的數據沒有被調用? Swift
func getUserData(hostViewController: UIViewController) {
if self.userID == nil{
println("User ID is nil")
}
else {
var key = self.userID
let urlString = UdacityClient.Constants.BaseURLSecure + "/users" + "/\(key)"
println(urlString)
let url = NSURL(string: urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
let request = NSMutableURLRequest(URL: url!)
let task = session.dataTaskWithRequest(request) {data, response, error in
if error != nil {
dispatch_async(dispatch_get_main_queue(), {() -> Void in
println(error)
let alertController = UIAlertController(title: "Oh no", message: "Error in User Data", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Retry", style: UIAlertActionStyle.Default, handler: { (alert) -> Void in
hostViewController.dismissViewControllerAnimated(true, completion: nil)
})
alertController.addAction(cancelAction)
hostViewController.presentViewController(alertController, animated: true, completion: nil)
})
println("Error in User Data")
} else {
let newData = data.subdataWithRange(NSMakeRange(5, data.length - 5))
var parsingError: NSError? = nil
let parsedResult = NSJSONSerialization.JSONObjectWithData(newData, options: NSJSONReadingOptions.AllowFragments, error: &parsingError) as! NSDictionary
if let userDictionary = parsedResult["user"] as? [String: AnyObject] {
if let firstName = userDictionary["first_name"] as? String {
self.firstName = firstName
println("This is not printing ")
println(firstName)
}
}
if let userDictionary = parsedResult["user"] as? [String: AnyObject] {
if let lastName = userDictionary["last_name"] as? String {
self.lastName = lastName
println("This is not printing also")
println(lastName)
}
}
if let err = parsingError {
dispatch_async(dispatch_get_main_queue(),{() -> Void in
println(err)
let alertController = UIAlertController(title: "Oh no", message: "Error in Parsing User Data from Udacity", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Retry", style: UIAlertActionStyle.Default, handler: { (alert) -> Void in
hostViewController.dismissViewControllerAnimated(true, completion: nil)
})
alertController.addAction(cancelAction)
hostViewController.presentViewController(alertController, animated: true, completion: nil)
})
}
}
}
task.resume()
}
}
我的請求URL:
https://www.udacity.com/api/users/Optional("3778758647")
我ResponseKeys結構:
struct JSONResponseKeys {
//getSessionID
static let Status = "status"
static let Account = "account"
static let Key = "key"
static let Session = "session"
static let ID = "id"
//getUserData
static let User = "user"
static let FirstName = "first_name"
static let LastName = "last_name"
}
致命錯誤而展開的可選值發生在我稱爲第一姓名和我的客戶的學生姓名如下:
UdacityClient.sharedInstance().firstName!) and UdacityClient.sharedInstance().lastName!
JSON解析結果控制檯日誌:
{
error = "Parameter 'user_key' key contains unescaped special character '\"': 'Optional(\"3778758647\")'";
parameter = "user_key";
status = 400;
}
log parsedResult並顯示結果。 – Shoaib
您是否嘗試過'userDictionary [「first_name」]作爲AnyObject!如?字符串' –
@Shoaib完成。請參閱更新後的問題。 –