swift
1import UIKit 2import TwitterKit 3 4class ViewController: UIViewController { 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 self.setNeedsStatusBarAppearanceUpdate(); 9 let logInButton = TWTRLogInButton { (session, error) in 10 if let unwrappedSession = session { 11 let alert = UIAlertController(title: "Logged In", 12 message: "User \(unwrappedSession.userName) has logged in", 13 preferredStyle: UIAlertControllerStyle.alert 14 ) 15 alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 16 self.present(alert, animated: true, completion: nil) 17 18 } else { 19 print("Login error: %@", error!.localizedDescription); 20 } 21 } 22 logInButton.loginMethods = [.webBased] 23 24 let button = logInButton 25 button.center = self.view.center 26 self.view.addSubview(button) 27 //ここからツイート投稿のための準備 28 let endpoint = "https://api.twitter.com/1.1/statuses/home_timeline.json" 29 30 let client = TWTRAPIClient.init(userID: "111111111") 31 32 var clientError : NSError? 33 34 let params = ["count": "10"] 35 36 let request = client.urlRequest(withMethod: "GET", url: endpoint, parameters: params, error: &clientError) 37 38 client.sendTwitterRequest(request) { 39 (response, data, connectionError) -> Void in 40 if connectionError != nil { 41 print("エラー: \(connectionError)") 42 } else { 43 44 let json: AnyObject? = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as AnyObject? 45 print(json!) 46 } 47 } 48 49 } 50}
このようにTwitterのタイムラインのJSONを受信することはできたのですが、Swiftでforeachで配列を回して、ツイート本文やユーザーネームを取り出して表示するにはどのようにすればいいのでしょうか?
JSONはこのような感じです
[
{
"place" : null,
"contributors" : null,
"source" : "<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>",
"truncated" : false,
"is_quote_status" : false,
"favorite_count" : 0,
"lang" : "ja",
"entities" : {
"symbols" : [
], "user_mentions" : [ ], "urls" : [ ], "hashtags" : [ ] }, "in_reply_to_screen_name" : null, "retweet_count" : 0, "favorited" : false, "geo" : null, "id" : 852734851786579974, "user" : { "protected" : false, "is_translator" : false, "profile_image_url" : "http:\/\/pbs.twimg.com\/profile_images\/850998804878774272\/-bABtccG_normal.jpg", "created_at" : "Mon Apr 05 08:12:28 +0000 2010", "id" : 129746204, "default_profile_image" : false, "listed_count" : 1542, "profile_background_color" : "131516", "follow_request_sent" : false, "location" : "PSO2", "entities" : { "url" : { "urls" : [ { "url" : "http:\/\/www.pixiv.net\/member.php?id=76712", "indices" : [ 0, 40 ], "expanded_url" : null } ] }, "description" : { "urls" : [ { "display_url" : "ask.fm\/patrietta", "url" : "https:\/\/t.co\/fLNYk70nQJ", "indices" : [ 67, 90 ], "expanded_url" : "http:\/\/ask.fm\/patrietta" } ] } }, "url" : "http:\/\/www.eeee.net\/member.php", "description" : "とあるuser", "followers_count" : 69707, "geo_enabled" : false, "lang" : "ja", "profile_text_color" : "333333", "statuses_count" : 55066, "following" : true, "notifications" : false, "profile_background_tile" : true, "profile_use_background_image" : true, "id_str" : "129746204", "name" : "あるuser", "profile_image_url_https" : "https:\/\/pbs.twimg.com\/profile_images\/850998804878774272\/-yff8f6fyh.jpg", "profile_sidebar_fill_color" : "EFEFEF", "profile_sidebar_border_color" : "FFFFFF", "contributors_enabled" : false, "default_profile" : false, "profile_banner_url" : "https:\/\/pbs.twimg.com\/profile_banners\/129746204\/1471936684", "screen_name" : "ここがスクリーンネーム", "time_zone" : "Tokyo", "profile_background_image_url" : "http:\/\/pbs.twimg.com\/profile_background_images\/454304509376741376\/lTp51aDO.jpeg", "profile_background_image_url_https" : "https:\/\/pbs.twimg.com\/profile_background_images\/454304509376741376\/lTp51aDO.jpeg", "profile_link_color" : "F5ABB5", "favourites_count" : 3889, "is_translation_enabled" : false, "translator_type" : "none", "utc_offset" : 32400, "friends_count" : 1233, "verified" : false, "has_extended_profile" : true }, "in_reply_to_user_id" : null, "retweeted" : false, "text" : "ツイート本文", "created_at" : "Fri Apr 14 04:06:46 +0000 2017", "in_reply_to_status_id_str" : null, "in_reply_to_status_id" : null, "in_reply_to_user_id_str" : null, "id_str" : "852734851786579974", "coordinates" : null
}
]
回答1件
あなたの回答
tips
プレビュー