我一直在阅读一些有类似问题的问题的答案,但我无法弄明白......
我有PostService执行JSON POST请求并从MySQL数据库中获取数据。在我转换到Swift 2.0之前一切正常,现在它给了我齿轮。 (代码来自Skip Wilson的Youtube系列 - Swift: Using External Databases and API's)
它在输出中给出上述错误并停止并突出显示该行 -
“let response =(try!NSJSONSerialization.JSONObjectWithData(data!,options:NSJSONReadingOptions.MutableContainers))as!NSDictionary”
var settings:Settings!
init() {
self.settings = Settings()
}
let userLoginEmail = "admin@email.co.za";
let userLoginPassword = "1234";
func getPosts(callback:(NSDictionary) -> ()) {
request(settings.viewPosts, callback: callback)
}
func request(url:String, callback:(NSDictionary) -> ()) {
let myURL = NSURL(string: url)
let requested = NSMutableURLRequest(URL:myURL!);
requested.HTTPMethod = "POST";
let postString = "email=\(userLoginEmail)&password=\(userLoginPassword)";
print("email=\(userLoginEmail)&password=\(userLoginPassword)")
requested.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(requested) {
(data, response, error) in
let response = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
callback(response)
}
task.resume()
}
这是我的JSON帖子...由于我的知识有限并且在json.org上阅读,它看起来像一个包含一堆对象的数组的对象(一个字典?)所有这些的格式都没有更改,我的应用程序将从数据库中获取数据并在转换前正确显示..
{"posts":[{"Post":{"Id":"5","idno":"4","product":"Aspen Simvastatin","quantity":"30","due_date":"2015-04-11","last_repeat":"2015-04-10","doctor":"Dr. Jim Jones","store":"Central","currentrepeat":"2","totalrepeat":"6","active_ingredient":"Simvastatin","strength":"20mg","manufacturer":"Aspen Pharmacare","dosage_form":"Tabs","directions":"Take one tablet daily","repeatflag":"0","repeattimestamp":"2015-08-17 20:38:13"}},{"Post":{"Id":"6","idno":"4","product":"Mybulen","quantity":"45","due_date":"2015-04-11","last_repeat":"2015-04-10","doctor":"Dr. Jim Jones","store":"Central","currentrepeat":"3","totalrepeat":"6","active_ingredient":"Codeine Phosphate;Ibuprofen;Paracetamol","strength":"10mg;200mg;250mg","manufacturer":"Aspen Pharmacare","dosage_form":"Tabs","directions":"Take one or two tablets four times a day after meals","repeatflag":"0","repeattimestamp":"2015-08-17 20:38:13"}},{"Post":{"Id":"7","idno":"4","product":"Ecotrin XL","quantity":"30","due_date":"2015-04-11","last_repeat":"2015-03-11","doctor":"Dr. Jim Jones","store":"Central","currentrepeat":"4","totalrepeat":"6","active_ingredient":"Aspirin","strength":"81mg","manufacturer":"Litha Pharma","dosage_form":"Tabs","directions":"Take one tablet in the morning","repeatflag":"0","repeattimestamp":"2015-08-17 20:38:13"}},{"Post":{"Id":"8","idno":"4","product":"Lorien","quantity":"28","due_date":"2015-04-11","last_repeat":"2015-03-11","doctor":"Dr. J. Eckel","store":"Central","currentrepeat":"4","totalrepeat":"6","active_ingredient":"Fluoxetine HCl","strength":"20mg","manufacturer":"Aspen Pharmacare","dosage_form":"Caps","directions":"Take one capsule in the morning","repeatflag":"0","repeattimestamp":"2015-08-17 20:38:13"}}]}
如果有任何帮助,我将非常感激。
在我的masterViewController的viewDidLoad()中,我有这个代码处理获取的信息......
service = PostService()
service.getPosts {
(response) in
self.loadPosts(response["posts"]! as! NSArray)
}
}
func loadPosts(posts:NSArray) {
for post in posts {
let post = post["Post"]! as! NSDictionary
let Id = Int((post["Id"]! as! String))!
let idno = Int((post["idno"]! as! String))!
let product = post["product"]! as! String
let quantity = Int((post["quantity"]! as! String))!
let doctor = post["doctor"]! as! String
let store = post["store"]! as! String
let currentrepeat = Int((post["currentrepeat"]! as! String))!
let totalrepeat = Int((post["totalrepeat"]! as! String))!
let active_ingredient = post["active_ingredient"]! as! String
let strength = post["strength"]! as! String
let manufacturer = post["manufacturer"]! as! String
let dosage_form = post["dosage_form"]! as! String
let directions = post["directions"]! as! String
let postObj = Post(Id: Id, idno: idno, product: product, quantity: quantity, doctor: doctor, store: store, currentrepeat: currentrepeat, totalrepeat: totalrepeat, active_ingredient: active_ingredient, strength: strength, manufacturer: manufacturer, dosage_form: dosage_form, directions: directions)
postsCollection.append(postObj)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
答案 0 :(得分:2)
您告诉NSJSONSerialization您绝对相信JSON可以被解析,并且您希望应用程序崩溃,如果它没有。 (那是尝试!)。好吧,有很多情况下你要求JSON并且你得到html,所以你的用户不会对此感到满意,让我们说如果他们在酒店或在酒店使用你的应用程序最近的星巴克。
接下来,您告诉NSJSONSerialization您绝对确定JSON包含字典,并且如果它不是(NSDictionary),您希望应用程序崩溃。猜猜看,你得到了一个数组。您最好阅读API的文档,并检查您在此处给出的内容。
顺便说一句。我不在乎你发布了什么JSON你应该得到什么 - 我知道你收到了一个阵列。不相信吗?调试的第一条规则:你知道的是错的。