我正在尝试将包含另一个自定义对象的自定义对象保存到UserDefault
,但我一直在
'无法识别的选择器已发送到实例'
错误。
我已经为这两个类实现了NSCoding
协议。
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: details)
我正在尝试存档RideDetails类并将其保存到UserDefault
。
我在另一个项目中做了类似的事情,它没有包含嵌套的自定义对象,并且工作正常。可能是因为它没有工作的嵌套对象。我无法弄明白。
DriverProfile Class
class DriverProfile: NSObject, NSCoding{
private var userId: Int
private var driverId:Int
private var firstName: String
private var lastName: String
private var phone: String
private var email:String?
private var location: CLLocation?
private var vehicleType: String
private var vehicleNo: String
private var rating:Double?
private var imageName:String?
init(userId: Int, driverId: Int, firstName:String, lastName: String, phone:String, email:String?, location:CLLocation?,vehicleType:String,vehicleNo:String,rating:Double?,imageName:String?) {
self.userId = userId
self.driverId = driverId
self.firstName = firstName
self.lastName = lastName
self.phone = phone
self.email = email
self.location = location
self.vehicleType = vehicleType
self.vehicleNo = vehicleNo
self.rating = rating
self.imageName = imageName
}
func encode(with aCoder: NSCoder) {
aCoder.encode(userId, forKey:"userId")
aCoder.encode(driverId, forKey:"driverId")
aCoder.encode(firstName, forKey:"firstName")
aCoder.encode(lastName, forKey:"lastName")
aCoder.encode(phone, forKey:"phone")
aCoder.encode(email, forKey:"email")
aCoder.encode(location, forKey:"location")
aCoder.encode(vehicleType, forKey:"vehicleType")
aCoder.encode(vehicleNo, forKey:"vehicleNo")
aCoder.encode(rating, forKey:"rating")
aCoder.encode(imageName, forKey:"imageName")
}
required init?(coder aDecoder: NSCoder) {
userId = aDecoder.decodeInteger(forKey: "userId")
driverId = aDecoder.decodeInteger(forKey: "driverId")
firstName = aDecoder.decodeObject(forKey: "firstName") as! String
lastName = aDecoder.decodeObject(forKey: "lastName") as! String
phone = aDecoder.decodeObject(forKey: "phone") as! String
email = aDecoder.decodeObject(forKey: "email") as? String
location = aDecoder.decodeObject(forKey: "location") as? CLLocation
vehicleType = aDecoder.decodeObject(forKey: "vehicleType") as! String
vehicleNo = aDecoder.decodeObject(forKey: "vehicleNo") as! String
rating = aDecoder.decodeObject(forKey: "rating") as? Double
imageName = aDecoder.decodeObject(forKey: "imageName") as? String
super.init()
}
}
RideDetails类
class RideDetails: NSObject{
private var rideTime:String?
private var rideId:Int
private var driverProfile:DriverProfile
private var destinationLocation: CLLocation?
private var myLocation:CLLocation?
private var otpCode:String
private var trackRideURL:String
private var customerId:Int
private var extendedRange:Bool?
private var extendedRangePrice:Double?
init(rideTime:String?,rideId:Int,driverProfile:DriverProfile,destinationLocation:CLLocation?,myLocation:CLLocation?,otpCode:String,trackRideURL:String,customerId:Int,extendedRange:Bool?,extendedRangePrice:Double?) {
self.rideTime = rideTime
self.rideId = rideId
self.driverProfile = driverProfile
self.destinationLocation = destinationLocation
self.myLocation = myLocation
self.otpCode = otpCode
self.trackRideURL = trackRideURL
self.customerId = customerId
self.extendedRange = extendedRange
self.extendedRangePrice = extendedRangePrice
}
func encode(with aCoder: NSCoder) {
aCoder.encode(rideTime, forKey:"rideTime")
aCoder.encode(rideId, forKey:"rideId")
aCoder.encode(driverProfile, forKey:"driverProfile")
aCoder.encode(destinationLocation, forKey:"destinationLocation")
aCoder.encode(myLocation, forKey:"myLocation")
aCoder.encode(otpCode, forKey:"otpCode")
aCoder.encode(trackRideURL, forKey:"trackRideURL")
aCoder.encode(customerId, forKey:"customerId")
aCoder.encode(extendedRange, forKey:"extendedRange")
aCoder.encode(extendedRangePrice, forKey:"extendedRangePrice")
}
required init?(coder aDecoder: NSCoder) {
rideTime = aDecoder.decodeObject(forKey: "rideTime") as? String
rideId = aDecoder.decodeInteger(forKey: "rideId")
driverProfile = aDecoder.decodeObject(forKey: "driverProfile") as! DriverProfile
destinationLocation = aDecoder.decodeObject(forKey: "destinationLocation") as? CLLocation
myLocation = aDecoder.decodeObject(forKey: "myLocation") as? CLLocation
otpCode = aDecoder.decodeObject(forKey: "otpCode") as! String
trackRideURL = aDecoder.decodeObject(forKey: "trackRideURL") as! String
customerId = aDecoder.decodeInteger(forKey: "customerId")
extendedRange = aDecoder.decodeObject(forKey: "extendedRange") as? Bool
extendedRangePrice = aDecoder.decodeObject(forKey: "extendedRangePrice") as? Double
super.init()
}
}