我已经从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完成。请参阅更新后的问题。 –