【问题标题】:Swift Alamofire Unexpectedly found nil - Facebook APISwift Alamofire 意外发现 nil - Facebook API
【发布时间】:2015-10-09 02:43:13
【问题描述】:

我正在使用 Alamofire 和 SwiftyJSON 为 Instagram 和 Youtube API 运行相同的功能;但是,使用 Facebook 的 API 时出现致命错误:在展开 Optional 值时意外发现 nil。

    var posts : [JSON] = []

    func loadFBFeed() {
    let url = "https://graph.facebook.com/mpstnews/posts/?access_token=\(apiKey)&date_format=U&fields=comments.limit(0).summary(1),likes.limit(0).summary(1),from,picture,message,story,name,link,created_time,full_picture&limit=5"

    Alamofire.request(.GET, url).responseJSON { (request, response, json) -> Void in
        switch json {
        case .Success(let data):
            let jsonObj = JSON(data)
            if let postInfo = jsonObj["data"].arrayValue as [JSON]? {
                self.posts = postInfo
                self.tableView.reloadData()
            }
        case .Failure(_, let error):
            print("Request failed with error: \(error)")
        }
    }
}

【问题讨论】:

    标签: facebook swift facebook-graph-api alamofire swifty-json


    【解决方案1】:

    URL 使用了 Alamofire 不喜欢的字符,必须添加一行来解决...

        var urlString = "https://graph.facebook.com/mpstnews/posts/?access_token=\(apiKey)&date_format=U&fields=comments.limit(0).summary(1),likes.limit(0).summary(1),from,picture,message,story,name,link,created_time,full_picture&limit=5"
    
        urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
    

    【讨论】:

      【解决方案2】:

      这通常发生在您的网址无效时。您插入 URL 的值之一很可能是错误的,导致 URLStringConvertible 逻辑崩溃。

      【讨论】:

      • 但是当我访问 URL 时,我得到了正确的 JSON 输出
      • 哪里出错了?是在.Failure 案例中打印出的错误吗?您能否发布确切的错误输出。
      • 看起来好像发生在 .Success 案例中。我已将错误输出的图像附加到问题中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多