Reputation: 565
I was trying to fetch Facebook group information, and I encountered an error I don't know how to fix.
When I try to access individual item for a Group's Feed, I cast a Feed from AnyObject to FBGraphObject, and this is where the error happens.
The error reads that "Thread 1 : EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0)"
I tried to do nil check, and a check on the type of the object to prevent the error, but it didn't fix the problem.
Any suggestion what might be the problem?
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
}
Upvotes: 2
Views: 2243
Reputation: 565
I fixed the problem by being more careful when parsing the FBGraphObject returned by the API call. It turned out I was wrong about assuming all Group Feeds returned by this API contain 'message' as a key.
Therefore the fixed version of my code works as follows:
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
}
}
So my lesson here is that I shouldn't rely too much on the XCode debugger's ability to pinpoint where exactly is the error coming from.
I don't know if this is because XCode6 is still in beta, or it's just the way it is, but one should be careful not to blindly trust the tool when working with a brand new language on top of a beta IDE.
Upvotes: 1