在 swift 中从 Facebook 图 Api 请求访问设备数据
Accessing devices data from Facebook graph Api request in swift
我在 swift 的 iphone 应用程序中使用 FBSDK 发出了图形请求,但我在访问它在结果中返回的一些信息时遇到了一些困难。具体来说,我想获取用户使用的设备平台列表。玩弄 Graph api 资源管理器,我获得了用于在用户设备上进行查询的数据。
{
"devices": [
{
"os": "iOS"
}
],
"id": "12345678912345"
}
但在 swift 中,当我将图形结果值打印到控制台时,返回的数据采用这种格式:
{
devices = (
{
os = iOS;
}
);
}
所以我的问题是,如何在 swift 中获取 'os' 的值?我所有将数据投射到 NSDictionaries 和 NSArrays 等的尝试都失败了。
let listOfDevices: String = result.valueForKey("devices") as? String
输出
devices = (
{
os = iOS;
}
);
这只是表明当我搜索 ["os"] 时它不包含 NSDictionary 的任何 key/pair 值,因为那些“()”括号。所有帮助表示赞赏。可能真的很简单...
我不想使用字符串正则表达式。
listOfDevices
应该是字典。
Swift 2
do {
// Replicating your data for the example
let response = "{\"devices\": [{\"os\": \"iOS\"}],\"id\": \"12345678912345\"}"
// Cast the response as a Dictionary with String as key and AnyObject as value
if let data = response.dataUsingEncoding(NSUTF8StringEncoding),
listOfDevices = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject] {
// Value of `devices` is an array of dictionaries
if let devices = listOfDevices["devices"] as? [[String:AnyObject]] {
for device in devices {
if let os = device["os"] as? String {
print(os)
}
}
}
// Value of `id` is a String
if let id = listOfDevices["id"] as? String {
// use `id`
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
Swift 1
// Replicating your data for the example
let response = "{\"devices\": [{\"os\": \"iOS\"}],\"id\": \"12345678912345\"}"
let data = response.dataUsingEncoding(NSUTF8StringEncoding)
// Cast the response as a Dictionary with String as key and AnyObject as value
let listOfDevices = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: nil) as! [String:AnyObject]
// Value of `devices` is an array of dictionaries
if let devices = listOfDevices["devices"] as? [[String:AnyObject]] {
for device in devices {
if let os = device["os"] as? String {
println(os)
}
}
}
// Value of `id` is a String
if let id = listOfDevices["id"] as? String {
// use `id`
}
我在 swift 的 iphone 应用程序中使用 FBSDK 发出了图形请求,但我在访问它在结果中返回的一些信息时遇到了一些困难。具体来说,我想获取用户使用的设备平台列表。玩弄 Graph api 资源管理器,我获得了用于在用户设备上进行查询的数据。
{
"devices": [
{
"os": "iOS"
}
],
"id": "12345678912345"
}
但在 swift 中,当我将图形结果值打印到控制台时,返回的数据采用这种格式:
{
devices = (
{
os = iOS;
}
);
}
所以我的问题是,如何在 swift 中获取 'os' 的值?我所有将数据投射到 NSDictionaries 和 NSArrays 等的尝试都失败了。
let listOfDevices: String = result.valueForKey("devices") as? String
输出
devices = (
{
os = iOS;
}
);
这只是表明当我搜索 ["os"] 时它不包含 NSDictionary 的任何 key/pair 值,因为那些“()”括号。所有帮助表示赞赏。可能真的很简单...
我不想使用字符串正则表达式。
listOfDevices
应该是字典。
Swift 2
do {
// Replicating your data for the example
let response = "{\"devices\": [{\"os\": \"iOS\"}],\"id\": \"12345678912345\"}"
// Cast the response as a Dictionary with String as key and AnyObject as value
if let data = response.dataUsingEncoding(NSUTF8StringEncoding),
listOfDevices = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject] {
// Value of `devices` is an array of dictionaries
if let devices = listOfDevices["devices"] as? [[String:AnyObject]] {
for device in devices {
if let os = device["os"] as? String {
print(os)
}
}
}
// Value of `id` is a String
if let id = listOfDevices["id"] as? String {
// use `id`
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
Swift 1
// Replicating your data for the example
let response = "{\"devices\": [{\"os\": \"iOS\"}],\"id\": \"12345678912345\"}"
let data = response.dataUsingEncoding(NSUTF8StringEncoding)
// Cast the response as a Dictionary with String as key and AnyObject as value
let listOfDevices = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: nil) as! [String:AnyObject]
// Value of `devices` is an array of dictionaries
if let devices = listOfDevices["devices"] as? [[String:AnyObject]] {
for device in devices {
if let os = device["os"] as? String {
println(os)
}
}
}
// Value of `id` is a String
if let id = listOfDevices["id"] as? String {
// use `id`
}