问题,同时迅速从2.3迁移到SWIFT 3
我已经更新了我的代码从SWIFT 2.3至3迅速得到&许多错误移除所有的人,但来到这里卡住问题,同时迅速从2.3迁移到SWIFT 3
我得到错误的didSelectRowAt indexPath:中IndexPath UITableView的
任务:我打了数据API和实现代码如下其工作正常填充数据,但我想保存这一切是在swift2.3为我工作在didSelectRowAtIndexPath方法数据
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 …
}
AAAAH最后我得到了它在SWIFT 3.0这个功能func encodeWithCoder(aCoder: NSCoder) {}
改为在Solution for this感谢您的时间得到了refrence和帮助阵列self.searchflightsandfares的@vadian
如果您searchflightsandfares数组类型是
let searchflightsandfares = [SearchFlightsAndFares]()
,那么你需要写这个
data = NSKeyedArchiver.archivedData(withRootObject: searchflightsandfares[indexPath.row]) as NSData
希望它会帮助你
我有类对不起后,我没有写过'var searchflightsandfares = [SearchFlightsAndFares]()' – Sunil
不需要额外的声明行。这不是Objective-C。评论一下。
// let data: Data
数组中的项目应该是铸造而不是归档对象。所以把右括号放到最后。
let data = NSKeyedArchiver.archivedData(withRootObject: self.searchflightsandfares[indexPath.row] as SearchFlightsAndFares)
如果您的数据源阵列申报的具体雨燕([SearchFlightsAndFares]
)你不需要投什么。
我尝试了很多方法,尝试了@jignesh建议的方式,所有这些代码都在swift 2.3上工作我不知道为什么它不在这里工作,我是如果我用你的代码'[projectName.SearchFlightsAndFares encodeWithCoder:]替换它:无法识别的选择器发送到实例0x7f9f20033800' – Sunil
你应该添加'NSCo在自定义类中采用。 – vadian
已在nsobject类中具有'必需的init(编码器aDecoder:NSCoder)'和'func encodeWithCoder(aCoder:NSCoder)'。是你问我做什么.....我不知道哪些自定义类我必须做这个除此之外我只有一个其他类,即'SearchFlightsAndFaresCellTableViewCell:UITableViewCell'与'var searchFlightsAndFares:SearchFlightsAndFares! {0}更新来自模型类的数据,即SearchFlightsAndFares – Sunil
加减速码 –