我的目标是首先在视图控制器中使用json数据实现一个viewtable。 然后在另一个viewcontroller中我想从表中选择一些记录来查看。
首先我的班级路线在
之上class Route {
var id: Int?
var travelTimeSeconds: Int?
var condition:String?
var lastUpdate:String?
var title:String?
var backup = [Route]()
init(json: NSDictionary) {
self.id = json["id"] as? Int
self.travelTimeSeconds = json["travelTimeSeconds"] as? Int
self.condition = json["condition"] as? String
self.lastUpdate = json["lastUpdate"] as? String
self.title = json["title"] as? String
}
init(){}
func findRoutes(predferedroute:String)->[Route] {
let jsonurl = NSURL(string: predferedroute)!
let task = NSURLSession.sharedSession().dataTaskWithURL(jsonurl) { (data,response, error) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if let urlcontent = data {
do{
let jsonresult = try NSJSONSerialization.JSONObjectWithData(urlcontent, options: NSJSONReadingOptions.MutableContainers)
if let routeArray = jsonresult["result"] as? [NSDictionary] {
for item in routeArray {
self.backup.append(Route(json: item))
}//end for
}//end if
}//end do
catch {
print("Serialization error")
}//end catch
}//end if
})//end dispatch
}//end task
task.resume()
return self.backup
}//end findRoutes
} 在第一个视图cotroller我写下面的代码
class MenuPageViewController: UIViewController{
var routes = [Route]()
var implementroutes = Route()
var allroutes:String = "http://quickweb.gr/itsweb/webservices/json?op=GetAll"
var activeRoutes:String = "http://quickweb.gr/itsweb/webservices/json?op=GetActive"
override func viewDidLoad() {
super.viewDidLoad()
NSUserDefaults.standardUserDefaults().setObject("palios55", forKey: "StartUp")
let nickname1 = NSUserDefaults.standardUserDefaults().objectForKey("StartUp")!
print(nickname1)
routes = implementroutes.findRoutes(allroutes)
routes = implementroutes.backup
}//end viewload
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
表视图的其他委托工作 但仍然保持两个代码的路线为零:
routes = implementroutes.findRoutes(allroutes) routes = implementroutes.backup
我是否想念viewcontrollers和类? 如果我直接在tableview中使用相同的代码,它就可以工作。