gpt4 book ai didi

ios - Swift Json - 从 API 获取数据

转载 作者:行者123 更新时间:2023-11-28 07:09:00 25 4
gpt4 key购买 nike

当我尝试从网络 API 获取数据时遇到问题。但我得到的是零。我无法从 api 获取文章列表。有什么建议吗?

api文档地址:yazar.io/doc/

我的 NSObject 文件:

import Alamofire
struct YazarIoService {

// Doc http://yazar.io/doc/
private static let baseURL = "http://yazar.io"
private enum ResourcePath: Printable {

case Articles
case StoryUpvote(storyId: Int)
var description: String {
switch self {
case .Articles: return "/api/article/feed/1"
case .StoryUpvote(let id): return "/api/client/favorites/action/"
}
}
}

static func articleForSection(section: String, page: Int, response: (JSON) -> ()){

let urlString = baseURL + ResourcePath.Articles.description
println(urlString)
let parameters = [
"page": toString(page)
]
println(parameters)

Alamofire.request(.GET, baseURL, parameters: parameters).responseJSON{(_, _, data, _) -> Void in
let articles = JSON(data ?? [])
response(articles)
}
}

我的 TableView Controller ;

class articlesTableViewController: UITableViewController, MakaleTableViewCellDelegate {

let transitionManager = TransitionManager()
var articles: JSON! = []

func loadarticles(section: String, page: Int){
YazarIoService.articleForSection(section, page: page) { (JSON) -> () in
self.articles = JSON["articles"]
self.tableView.reloadData()
}
}

override func viewDidLoad() {
super.viewDidLoad()


UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension

loadarticles("", page: 1)
println(articles[])
}

@IBAction func MenuButtonDidTouch(sender: AnyObject) {
performSegueWithIdentifier("MenuSegue", sender: self)
}


@IBAction func LoginbutonDidTouch(sender: AnyObject) {
performSegueWithIdentifier("LoginSegue", sender: self)
}

//number of rows
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return articles.count //
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MakaleCell") as MakaleTableViewCell //--> cell'in özelliklerine erişmek içim bunu yazdık. Eskiden UITableViewCell'di. Değiştirmezsen cell.title... vs çalışmakz.

let makale = articles[indexPath.row]
cell.configureWithMakale(makale)

cell.delegate = self

return cell
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("WebSegue", sender: indexPath)

tableView.deselectRowAtIndexPath(indexPath, animated: true)
}


// MARK: MakaleTableViewCellDelegate

func makaleTableViewCellDidTouchUpvote(cell: MakaleTableViewCell, sender: AnyObject) {
// TODO: Implement Upvote
}

func makaleTableViewCellDidTouchComment(cell: MakaleTableViewCell, sender: AnyObject) {
performSegueWithIdentifier("CommentsSegue", sender: self)
}

// MARK: Misc

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "WebSegue" {
let toView = segue.destinationViewController as WebViewController
let indexPath = sender as NSIndexPath
let url = articles[indexPath.row]["article_url"].string!
toView.url = url


UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Fade)

toView.transitioningDelegate = transitionManager
}
}

}

最佳答案

此链接http://yazar.io/api/newspaper/1给出 404 错误,所以很明显你会从请求中返回一个 nil。

关于ios - Swift Json - 从 API 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29232890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com