gpt4 book ai didi

xcode - Swift 错误解析 Facebook Graph API 对象 : EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP)

转载 作者:搜寻专家 更新时间:2023-11-01 05:31:26 24 4
gpt4 key购买 nike

我在尝试获取 Facebook 群组信息时遇到错误,我不知道如何修复。

当我尝试访问群组提要的单个项目时,我将提要从 AnyObject 转换为 FBGraphObject,这就是错误发生的地方。

错误显示为“Thread 1 : EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0)”

我尝试进行 nil 检查,并检查对象的类型以防止错误,但并没有解决问题。

任何建议可能是什么问题?

func fetchFacebookGroup() {
FBRequestConnection.startWithGraphPath("/\(groupId)/?fields=feed", completionHandler: {(connection: FBRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
if result? {
NSLog("error = \(error)")

var jsonFeeds = result as FBGraphObject
self.feeds = self.buildFeeds((jsonFeeds["feed"] as FBGraphObject)["data"] as NSMutableArray)
self.tableView.reloadData()
}
} as FBRequestHandler)

}

func buildFeeds(data: NSMutableArray) -> Array<GroupFeed> {
var result : Array<GroupFeed> = []
for rawFeed : AnyObject in data {
if rawFeed is FBGraphObject { // This is where error pops up.
if let jsonFeed = rawFeed as? FBGraphObject {
var feed = GroupFeed(id: jsonFeed["id"] as String, name: (jsonFeed["from"] as FBGraphObject)["name"] as String, message: jsonFeed["message"] as String, updatedTime: jsonFeed["updated_time"] as String)
result.append(feed)
}
}
}

return result
}

最佳答案

我在解析 API 调用返回的 FBGraphObject 时更加小心,从而解决了这个问题。事实证明,我错误地假设此 API 返回的所有群组订阅源都包含“消息”作为键。

因此我的代码的固定版本工作如下:

TableViewController.swift

func fetchFacebookGroup() {
FBRequestConnection.startWithGraphPath("\(groupId)/?fields=feed.limit=(1)", completionHandler: {(connection: FBRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
if result? {
NSLog("error = \(error)")

var jsonFeeds = result as FBGraphObject
self.feeds = self.buildFeeds((jsonFeeds["feed"] as FBGraphObject)["data"] as NSMutableArray)
self.tableView.reloadData()
}
} as FBRequestHandler)

}

func buildFeeds(data: NSMutableArray) -> Array<GroupFeed> {
var result : Array<GroupFeed> = []
for rawFeed : AnyObject in data {
if rawFeed is FBGraphObject {
if let jsonFeed = rawFeed as? FBGraphObject {
var feed = GroupFeed(jsonFeed: jsonFeed)
result += feed
}
}
}
return result
}

GroupFeed.swift

class GroupFeed: NSObject {
var id : String!
var name : String!
var message : String?
var updatedTime : String!

init(jsonFeed: FBGraphObject) {
self.id = jsonFeed["id"] as String
self.name = (jsonFeed["from"] as FBGraphObject)["name"] as String
if let message = jsonFeed.objectForKey("message") as? String {
self.message = message
}
self.updatedTime = jsonFeed["updated_time"] as String
}
}

所以我在这里的教训是,我不应该过分依赖 XCode 调试器的能力来查明错误的确切来源。

我不知道这是因为 XCode6 仍处于测试阶段,还是它就是这样,但是在测试版 IDE 上使用一种全新的语言时,应该注意不要盲目信任该工具.

关于xcode - Swift 错误解析 Facebook Graph API 对象 : EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24473933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com