我想學習在swift3中使用ClassModels,因爲我找不到合適的教程來理解基本概念,我提到了太多的網站,併成功地創建了模型類併爲模型設置了值類的對象從樣本解析的JSON值。我可以在一個UIViewcotroller中設置ModelClass並使用swift3 iOS從另一個UIViewCotroller讀取ModelClass的對象嗎?
我的模型類,
import UIKit
import SwiftyJSON
class TrainObject {
var towards: String!
var lastUpdated: String!
var delay: String!
var trainName: String!
var lastLocation: String!
var trainNo: String!
var upcomingStationInfo: String!
required init(json: JSON) {
lastUpdated = json["lastUpdated"].stringValue
trainName = json["trainName"].stringValue
}
}
從ListViewController(UIViewController中)的viewDidLoad中
現在,我使用Alamofire並使用代碼設置的結果傳送給模型類,解析JSON從URL
if let results = json["info"]["trainData"].array {
print(results)
for entry in results {
self.items.append(TrainObject(json: entry))
}
print(self.items)
let user = self.items[0]
print(user.trainName)
self.tableOfInfo?.reloadData()
}
從ListViewController的的tableview委託
此外,我得到的數組值逐一在tableview中像被加載,
let user = self.items[indexPath.row]
print(user.trainName)
print(user.delay)
現在在tableview選擇上,我可以使用通常的代碼將適當的值傳遞給另一個名爲DetailsViewController的UIViewController。我的問題是,有沒有辦法直接從DetailsViewController訪問模型類(TrainObject),然後使用選定的indexpath.row值獲取適當的數組?或者我應該從tableview的didselectrow方法傳遞數組本身?我在這裏感到困惑你能解釋一下模型類的真正用途嗎?我應該如何以及在哪裏使用它?
其[TrainObject]和傳遞單個對象可以使用TrainObject到其他的viewController –
可以請你的代碼解釋你的ListView? – swiftDevStart
確保您要將選定的索引數據傳遞給detailViewController?來自[TrainObject] –