首页 文章

Swift 2.0:无法将'__NSArrayM'(0x10a9348d8)类型的值转换为'NSDictionary'(0x10a934d60)

提问于
浏览
1

我一直在阅读一些有类似问题的问题的答案,但我无法弄明白......

我有PostService执行JSON POST请求并从MySQL数据库中获取数据 . 在我转换到Swift 2.0之前一切正常,现在它's giving me gears. (Code comes from Skip Wilson'的Youtube系列 - Swift: Using External Databases and API's

它在输出中给出上述错误并停止并突出显示此行 -

“让响应=(尝试!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()
        }
    }

1 回答

  • 2

    您告诉NSJSONSerialization您绝对确定可以解析JSON并且您希望应用程序崩溃(如果不是) . (这是尝试!) . 好吧,有很多情况下你要求JSON并且你得到了html,所以你的用户会对此不满意,假设他们在酒店或最近的星巴克使用你的应用程序 .

    接下来,您告诉NSJSONSerialization您绝对确定JSON包含字典,并且您希望您的应用程序崩溃(如果不是!NSDictionary) . 猜猜看,你得到了一个数组 . 您最好阅读API的文档,并检查您在此处给出的内容 .

    BTW . 我不在乎你发布的是你应该得到的JSON - 我知道你收到了一个阵列 . 不相信吗?调试的第一条规则:你知道的是错的 .

相关问题