在swift错误中解析JSON"致命错误:在展开可选值时意外发现nil"

时间:2015-06-22 07:13:07

标签: json swift

我收到了错误"致命错误:在打开一个Optional值时意外发现nil"当我在完成后调试。

任何人都知道如何修复它?

我的API函数:

class nsAPI {

    func loadPosts(completion: ((AnyObject) -> Void)!) {

        var urlString = "http://nhacsan.tv/api/songs/"

        let session = NSURLSession.sharedSession()
        let postURL = NSURL(string: urlString)

        var task = session.dataTaskWithURL(postURL!) {
            (data, response, error) -> Void in

            if error != nil {
                println(error.localizedDescription)
            }else {

                var error : NSError?

                var postsData = (NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error:nil) as! NSDictionary)["Posts"] as! NSMutableArray

                var posts = [Post]()
                for post in postsData{
                    let post = Post(data: post as! NSDictionary)
                    posts.append(post)
                }

                let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
                    dispatch_async(dispatch_get_global_queue(priority, 0)) {
                    dispatch_async(dispatch_get_main_queue()){
                        completion(posts)
                    }
                }
            }
        }
        task.resume()
    }
}

0 个答案:

没有答案