Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 显示推特时间线,但为空_Ios_Swift_Twitter_Slrequest - Fatal编程技术网

Ios 显示推特时间线,但为空

Ios 显示推特时间线,但为空,ios,swift,twitter,slrequest,Ios,Swift,Twitter,Slrequest,我在社交媒体上练习swift,并在学习如何将Twitter时间线添加到你的应用程序中。一切似乎都很好,但唯一的问题是显示tweets的表是完全空的。代码如下所示 import UIKit import Social import Accounts class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tweetTableView:

我在社交媒体上练习swift,并在学习如何将Twitter时间线添加到你的应用程序中。一切似乎都很好,但唯一的问题是显示tweets的表是完全空的。代码如下所示

import UIKit
import Social
import Accounts

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


    @IBOutlet weak var tweetTableView: UITableView!

    var dataSource = [AnyObject]()


    func getTimeLine() {

        let account = ACAccountStore()
        let accountType = account.accountType(
        withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)

        account.requestAccessToAccounts(with: accountType, options: nil, completion: {(success, error) in

            if success {
                let arrayOfAccounts =
                account.accounts(with: accountType)

                if (arrayOfAccounts?.count)! > 0 {
                    let twitterAccount = arrayOfAccounts?.last as! ACAccount

                    let requestURL = URL(string:
                    "https://api.twitter.com/1.1/statuses/user_timeline.json")

                    let parameters = ["screen_name" : "@techotopia",
                                  "include_rts" : "0",
                                  "trim_user" : "1",
                                  "count" : "20"]

                    let postRequest = SLRequest(forServiceType:
                    SLServiceTypeTwitter,
                                            requestMethod: SLRequestMethod.GET,
                                            url: requestURL,
                                            parameters: parameters)

                    postRequest?.account = twitterAccount

                    postRequest?.perform(handler: {(responseData, urlResponse, error) in

                        do {
                            try self.dataSource = JSONSerialization.jsonObject(with: responseData!, options: JSONSerialization.ReadingOptions.mutableLeaves) as! [AnyObject]

                            if self.dataSource.count != 0 {
                                DispatchQueue.main.async() {
                                    self.tweetTableView.reloadData()
                                }
                            }
                        } catch let error as NSError {
                        print("Data serialization error: \(error.localizedDescription)")
                        }
                    })
                }
            } else {
                print("Failed to access account")
            }
        })
    }




    override func viewDidLoad() {
        super.viewDidLoad()

        tweetTableView.register(UITableViewCell.self,
                            forCellReuseIdentifier: "Cell")
        getTimeLine()
        tweetTableView.estimatedRowHeight = 50

        }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =
        tableView.dequeueReusableCell(withIdentifier: "Cell")
        let row = indexPath.row
        let tweet = self.dataSource[row]
        cell!.textLabel!.text = tweet.object(forKey: "text") as? String
        cell!.textLabel!.numberOfLines = 0
        return cell!

    }

}
结果如下。。。


我感谢你的回答!谢谢大家!

打印
responseData
它是空的吗?我在“do”操作之前打印了它。什么也没有出现。除非那不是打印响应的正确位置。打印
responseData
它是空的吗?我在“do”操作之前打印了它。什么也没有出现。除非那不是打印回复的正确位置。