我已将我的代码从swift 2.3更新为swift 3&得到了很多错误,所有错误都被删除了
我在didSelectRowAt indexPath上遇到错误:UITableView的索引路径
任务:我点击API获取数据并在tableview中填充数据,但是我想在didselectrowatindexpath上保存数据这一切都在swift2.3中为我工作
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected ")
//print(self.searchflightsandfares[indexPath.row] as SearchFlightsAndFares)
let data: NSData
// data = NSKeyedArchiver.archivedData(withRootObject: self.searchflightsandfares[indexPath.row].airlineCodeOneWay)
data = NSKeyedArchiver.archivedData(withRootObject: self.searchflightsandfares[indexPath.row]) as SearchFlightsAndFares
//Set Data
UserDefaults.standard.set(data, forKey: "selectedFlightDetailData")
SelectedFlightDetailsVC()
}
模型类是:
import UIKit
import Foundation
class SearchFlightsAndFares:NSObject{
//segment data
var segmentaDataForDetailScreen:[NSDictionary]
var fareDataFromFlight:Dictionary<String, AnyObject>
//Flight Details -One Way
var airlineCodeOneWay: String? //for list view
var airlineNameOneWay: String? //for list view
var airlineCodeOneWaySegment:String? //for Detail view
var airlineNameOneWaySegment:String? //for Detail view
….more code …
init(fareDataFromFlight: Dictionary<String, AnyObject>,
segmentaDataForDetailScreen: [NSDictionary],
airlineCodeOneWay: String, //for list view
airlineNameOneWay: String, //for list view
airlineCodeOneWaySegment: String, //Flight Number one way
airlineNameOneWaySegment: String,
….more code …
{
self.fareDataFromFlight = fareDataFromFlight
self.segmentaDataForDetailScreen = segmentaDataForDetailScreen
//One Way
self.airlineCodeOneWay = airlineCodeOneWay
self.airlineNameOneWay = airlineNameOneWay
self.airlineCodeOneWaySegment = airlineCodeOneWaySegment
}
init(espDictionary: [String : AnyObject])
{
logic for assignment from API to InIt Variables //….more code …
}
required init(coder aDecoder: NSCoder) {
self.fareDataFromFlight = (aDecoder.decodeObject(forKey: "fareDataFromFlight") as? Dictionary<String, AnyObject>)!
self.segmentaDataForDetailScreen = (aDecoder.decodeObject(forKey: "segmentaDataForDetailScreen") as? [NSDictionary])!
//One Way
self.airlineCodeOneWay = aDecoder.decodeObject(forKey: "airlineCodeOneWay") as? String
self.airlineNameOneWay = aDecoder.decodeObject(forKey: "airlineNameOneWay") as? String
self.airlineCodeOneWaySegment = aDecoder.decodeObject(forKey: "airlineCodeOneWaySegment") as? String
self.airlineNameOneWaySegment = aDecoder.decodeObject(forKey: "airlineNameOneWaySegment") as? String
….more code …
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encode(self.fareDataFromFlight, forKey: "fareDataFromFlight")
aCoder.encode(self.segmentaDataForDetailScreen, forKey: "segmentaDataForDetailScreen")
//One way
aCoder.encode(self.airlineCodeOneWay, forKey: "airlineCodeOneWay")
aCoder.encode(self.airlineNameOneWay, forKey: "airlineNameOneWay")
aCoder.encode(self.airlineCodeOneWaySegment, forKey: "airlineCodeOneWaySegment")
aCoder.encode(self.airlineNameOneWaySegment, forKey: "airlineNameOneWaySegment")
….more code …
}
答案 0 :(得分:1)
aaaah最后我得到了它在swift 3.0中这个函数func encodeWithCoder(aCoder: NSCoder) {}
改为func encode(with aCoder: NSCoder)
得到了Solution for this感谢您的时间和帮助@vadian
答案 1 :(得分:0)
如果您的searchflightsandfares数组类型是
let searchflightsandfares = [SearchFlightsAndFares]()
然后你需要写这个
data = NSKeyedArchiver.archivedData(withRootObject: searchflightsandfares[indexPath.row]) as NSData
希望它能帮到你