将对象保存为用户默认值

时间:2019-01-11 06:38:50

标签: ios swift

我在Struct下方,并且正在使用Struct创建默认对象。创建对象后,我将保存为用户默认值。但是得到错误。请参见下面的代码和错误。让我知道如何将该对象保存为用户默认值。

struct FlightSearchObj {
    var FlightTripType:Int = 0
    var FlightArrObj: [FlightObj]?
    var FlightTravellers: [String : Int] = ["Adults": 1, "Child": 0, "Infants": 0]
    var FlightClass : String =  "Economy"
    var PreferredAirline : [String] = []
    var FlightFareType : String = "All"
}

struct FlightObj {
    var fromAirportObj:AirportDetail?
    var toAirportObj:AirportDetail?
    var DepartDate: String?
}

在ViewController中

var finalOneWayRoundFlightSearchObj : FlightSearchObj?
var finalMultiFlightSearchObj : FlightSearchObj?



 //DefaultObject for Flights
finalOneWayRoundFlightSearchObj = FlightSearchObj()
finalMultiFlightSearchObj = FlightSearchObj()
var flightArrObj =  [FlightObj]()
let flightObj = FlightObj()
flightArrObj.append(flightObj)
flightArrObj.append(flightObj)

finalOneWayRoundFlightSearchObj?.FlightArrObj = flightArrObj
finalMultiFlightSearchObj?.FlightArrObj = flightArrObj

print(finalOneWayRoundFlightSearchObj?.FlightArrObj?.count ?? 0)
print(finalMultiFlightSearchObj?.FlightArrObj?.count ?? 0)



FetchOnewayRoundTripObjValues(obj:finalOneWayRoundFlightSearchObj!)
FetchMultiCityObjValues(obj:finalMultiFlightSearchObj!)


func FetchOnewayRoundTripObjValues(obj:FlightSearchObj){
    if (Constants.sharedInstance.defaults.value(forKey: "flightOnewayRoundTripObj") != nil){
        print("OneWay/RoundTrip Obj found - \(obj)")
    }else{
        print("OneWay/RoundTrip Obj not found")
        print(obj)
        Constants.sharedInstance.defaults.set(obj, forKey: "flightOnewayRoundTripObj")
    }


}
func FetchMultiCityObjValues(obj:FlightSearchObj){
    if (Constants.sharedInstance.defaults.value(forKey: "flightMultiFlightObj") != nil){
         print("MultiFlight Obj found - \(obj)")
    }else{
        print("MultiFlight Obj not found")
        Constants.sharedInstance.defaults.set(obj, forKey: "flightMultiFlightObj")
    }
}

错误 *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'试图插入非属性列表对象BookingApp.FlightSearchObj(FlightTripType:0,FlightArrObj:Optional([BookingApp.FlightObj(fromAirportObj:nil,toAirportObj:nil, DepartDate:无),BookingApp.FlightObj(从AirportObj:无,toAirportObj:无,DepartDate:无)]),FlightTravellers:[“ Adults”:1,“ Child”:0,“ Infants”:0],FlightClass:“ Economy “,首选航班:[],FlightFareType:“全部”),用于关键的FlightOnewayRoundTripObj' * 第一个调用堆栈: (0x202978ec4 0x201b49a40 0x20287f594 0x2029ab140 0x2029ab80c 0x2029ab580 0x2029ab930 0x202864408 0x2028f4908 0x2028f425c 0x202860db4 0x202863da4 0x2029b64d8 0x20333b4dc 0x103060038 0x1030644f8 0x102a56db8 0x102adcadc 0x102add8cc 0x22f6936d0 0x22f693b00 0x22f5ec2ec 0x22f600dac 0x22f602280 0x22f5e4470 0x23010373c 0x206f75b74 0x206f7ab2c 0x206ed944c 0x206f07d7c 0x206f08be4 0x2029087cc 0x202903460 0x202903a00 0x2029031f0 0x204b7c584 0x22fc5abc0 0x1030fb894 0x2023c2bb4) libc ++ abi.dylib:以类型为NSException的未捕获异常终止

2 个答案:

答案 0 :(得分:1)

在要保存在Codable中的所有结构中确认协议UserDefaults

struct FlightSearchObj: Codable {
    var FlightTripType:Int = 0
    var FlightArrObj: [FlightObj]?
    var FlightTravellers: [String : Int] = ["Adults": 1, "Child": 0, "Infants": 0]
    var FlightClass : String =  "Economy"
    var PreferredAirline : [String] = []
    var FlightFareType : String = "All"
}

struct FlightObj: Codable {
    var fromAirportObj:AirportDetail?
    var toAirportObj:AirportDetail?
    var DepartDate: String?
}

struct AirportDetail: Codable {
    // Your properties
}

现在,您需要使用UserDefaultsPropertyListEncoderPropertyListDecoder保存并获取该对象。 就您而言

func FetchOnewayRoundTripObjValues(obj:FlightSearchObj){
    if let data = UserDefaults.standard.value(forKey:"flightOnewayRoundTripObj") as? Data {
        let res = try? PropertyListDecoder().decode(FlightSearchObj.self, from: data)
        print("OneWay/RoundTrip Obj found - \(res)")
    }else{
        print("OneWay/RoundTrip Obj not found")
        print(obj)
        UserDefaults.standard.set(try? PropertyListEncoder().encode(obj), forKey:"flightOnewayRoundTripObj")
 //            Constants.sharedInstance.defaults.set(obj, forKey: "flightOnewayRoundTripObj")
    }


}
func FetchMultiCityObjValues(obj:FlightSearchObj){
    if let data = UserDefaults.standard.value(forKey:"flightOnewayRoundTripObj") as? Data {
        let res = try? PropertyListDecoder().decode(FlightSearchObj.self, from: data)
        print("MultiFlight Obj found - \(res)")
    }else{
        print("MultiFlight Obj not found")
        UserDefaults.standard.set(try? PropertyListEncoder().encode(obj), forKey:"flightMultiFlightObj")
 //            Constants.sharedInstance.defaults.set(obj, forKey: "flightMultiFlightObj")
    }
} 

答案 1 :(得分:0)

对于Struct,只需将其标记为采用Codable协议即可使结构可编码:

struct FlightSearchObj:Codable {
    var FlightTripType:Int = 0
    var FlightArrObj: [FlightObj]?
    var FlightTravellers: [String : Int] = ["Adults": 1, "Child": 0, "Infants": 0]
    var FlightClass : String =  "Economy"
    var PreferredAirline : [String] = []
    var FlightFareType : String = "All"
}

struct FlightObj:Codable {
    var fromAirportObj:AirportDetail?
    var toAirportObj:AirportDetail?
    var DepartDate: String?
}

还使AirportDetail可编码...