swift
1import UIKit 2import TwitterKit 3 4class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 let endpoint = "https://api.twitter.com/1.1/statuses/home_timeline.json" 8 let client = TWTRAPIClient.init(userID: "000000000") 9 var clientError : NSError? 10 let params = ["count": "3"] 11 let request = client.urlRequest(withMethod: "GET", url: endpoint, parameters: params, error: &clientError) 12 client.sendTwitterRequest(request) { 13 (response, data, connectionError) -> Void in 14 if connectionError != nil { 15 print("エラー: \(connectionError)") 16 } else { 17 let json = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [[String:Any]] 18 var myItems = [String]() 19 for obj in json { 20 let text = obj["text"] ?? "(empty)" 21 myItems += ["\(text)"] 22 } 23 } 24 } 25 } 26 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 27 return myItems.count 28 } 29 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 30 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) 31 cell.textLabel!.text = "\(myItems[indexPath.row])" 32 return cell 33 } 34}
このようにしてTwitterのツイートを取得し、配列に追加したのですが
for obj in json {
let text = obj["text"] ?? "(empty)"
myItems += ["(text)"]
}
このようなエラーが出て実行できません
どのようにすればセルに内容が表示されるかご教授いただけないでしょうか
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。