ios - Parsing twitter api JSON in Swift -
i using twitter-kit/fabric response twitter api. returns json of following format - https://dev.twitter.com/rest/reference/get/search/tweets
my query - https://api.twitter.com/1.1/search/tweets.json?q=""&geocode=lat,long,radius&count=15
i need extract text , coordinates json plot tweets on map.
what doing wrong following code - ?
twitter.sharedinstance().apiclient.sendtwitterrequest(request) { (response, data, connectionerror) -> void in if (connectionerror == nil) { var jsonerror : nserror? let parsedresult = nsjsonserialization.jsonobjectwithdata(data, options: nil, error: &jsonerror) nsdictionary println(parsedresult) if let tweets = parsedresult["statuses"] as? nsdictionary { tweetsdict in tweets { let title = tweetsdict["text"] nsstring println("text: \(title)") let coordinates = tweetsdict["coordinates"] println("coordinates: \(coordinates)\n") } }else { println("error: \(connectionerror)") } }
my code runs till println(parsedresult) i'm getting valid response twitter api. i'm having trouble extracting tweet text json response.
if using fabric
, can create tweet objects response using twtrtweet
method
+ (nsarray *)tweetswithjsonarray:(nsarray *)array;
it creates array of twtrtweet
instances array of twitter api json response.
nsarray *tweetdata = [twtrtweet tweetswithjsonarray:responsefromtwitter]; [tableview reloaddata];
you can use tweet objects populate in tableview in cellforrowatindexpath
. return tweet cell,
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellid = @"tweetcell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellid forindexpath:indexpath]; twtrtweet *tweet = twitterresponse[indexpath.row]; [(twtrtweettableviewcell *)cell configurewithtweet:tweet]; return cell; }
edit
in swift, can as:
// create array of tweet model objects json array tweetdata = twtrtweet.tweetswithjsonarray(responsefromtwitter) tableview.reloaddata() //create tweet cells override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let tweet = tweetdata[indexpath.row] let cell = tableview.dequeuereusablecellwithidentifier(tweettablereuseidentifier, forindexpath: indexpath) twtrtweettableviewcell cell.configurewithtweet(tweet) return cell }
hope helps!
Comments
Post a Comment