2015-10-09 21 views
1

我使用Alamofire和SwiftyJSON为Instagram和Youtube API运行相同的确切函数;然而,与Facebook的API我得到致命的错误:意外地发现零,同时展开一个可选值。Swift Alamofire意外地发现无 - Facebook API

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)") 
     } 
    } 
} 

Error output

回答

2

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())! 
0

当您的网址是无效的一般情况。很可能你插入到URL中的一个值很差,导致URLStringConvertible逻辑崩溃。

+0

但是,当我访问的URL,我得到了正确的JSON输出 – pmanning

+0

在哪儿发生的错误?是否在'.Failure'情况下打印出错误?你能发布确切的错误输出。 – cnoon

+0

它看起来好像是在.Success案例中发生的。我已将错误输出的图像附加到问题上。 – pmanning

相关问题