2016-07-23 23 views
2

所以,我想獲得所有其他Facebook朋友也列在我的應用程序連接列表。Swift Facebook獲取朋友圖表請求錯誤

做了FBSDKgraphrequest之後,我得到了正確的數據(見下面),然後將其轉換爲NSDictionary。數據是這樣的:

{ 
    friends =  { 
     data =   (
         { 
       id = 10154257515635281; 
       name = "Hector Judd"; 
      }, 
         { 
       id = 151132798649181; 
       name = "Arch Tester Dev"; 
      } 
     ); 
     paging =   { 
      cursors =    { 
       after = QVFIUndVRFZAINXhCSlAzdnNGeUUwTHdhamNpc3NFbjR2NjF4dk40N3ZAZAR2lNLXM0Q3BxLW90REVsaFk3aU13Um1Ca0NOd0ZAXN2gxaDF2emhYem9BMjhkMVl3; 
       before = QVFIUlo2a1Q4UGhpWmY2SFNWbUtpMVcxZAnEtR01KSlUyeVEtMU9GbjNkRHp2bTFKY0VoVm5xX3dNLXEwMG5OY0Q0My0ZD; 
      }; 
     }; 
     summary =   { 
      "total_count" = 831; 
     }; 
    }; 
    id = 10208811056967519; 
} 

然後我嘗試用多個解壓,如果讓語句,但最終得到的錯誤:段錯誤低於11見代碼。

func getFriends() { 

    let parameters = ["fields": "friends"] 

    FBSDKGraphRequest(graphPath: "me", parameters: parameters).startWithCompletionHandler({ (connection, result, error) -> Void in 

     if error != nil { 

      print(error) 

     } else { 

      if let result = result as? NSDictionary { 

       if let data = result["friends"] as? NSDictionary { 

        if let friendData = data["data"] as? NSArray { 

         for friend in friendData { 

          friendIds.append(friend["id"] as String) 

          print(friendIds) 

         } 


        } 
       } 

      } 
     } 

    }) 
} 

我對編程相對比較陌生,並且很努力地找出一個很好的方法來做到這一點。我們歡迎所有的建議!!謝謝。

回答

0

試圖聲明您friendData變量放入NSDictionary在循環

for friend in friends { 
     if let friendData = friend as? NSDictionary { 
       friendIds.append(friendData["id"] as! String) 
     } 
    }