gpt4 book ai didi

swift - 尝试获取 nil 值时解码器崩溃也编码解码枚举值不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 07:03:28 24 4
gpt4 key购买 nike

我有一个具有一些属性的类,我在保存时使用编码,在获取时使用解码器,但有些数据不是可选的,但在我第一次获取它时它是空的,所以当解码器调用和它发现 nill 值使应用程序崩溃。

所以我想要的是当我获取数据时,如果没有找到数据,我可以设置一些默认值而不是崩溃我可以通过将这些属性设为可选来解决这个问题,但我不希望那样。并且也无法添加 try catch 以确保它不会崩溃

请告诉我应该对此类进行哪些更改,并分享一些示例或链接或教程,深入解释有关编码器和解码器的所有内容(不是一些仅概述的媒体文章)

我是 swift 的新手,所以如果你发现这个类有什么问题,请在评论中告诉我,以及使用最新的 swift 的最佳方法

import Foundation

enum AcModeType {
case acModeCool
case acModeDry
case acModeHeat
case acModeFan
case acModeTimer
}

enum FanModeType {
case fanModeLow
case fanModeMid
case fanModeHigh
case FanModeAuto
}

class AcDevice: Device {
var brandId: String?
var variant: String?
var brandName: String?
var acTemperature: Int = 16
var acMinTemperature: Int = 16
var acMaxTemperature: Int = 32
var weatherTemperature: Int = 0
var roomTemperature: Int = 0
var timeTable = [TimeTable]()
var currentAcMode: AcModeType = .acModeCool
var currentFanMode: FanModeType = .fanModeLow
var lastPowerOnTime: Double = 0.0
var isPowerOn = false
var isSleepOn = false
var isTimeTableOn = false
var hasScreen = false

required public init?(coder aDecoder: NSCoder) {
super.init();
brandId = (aDecoder.decodeObject(forKey: "brandId") as! String?)
variant = (aDecoder.decodeObject(forKey: "variant") as! String?)
brandName = (aDecoder.decodeObject(forKey: "brandName") as! String?)
acTemperature = (aDecoder.decodeObject(forKey: "temperature") as! Int)
acMinTemperature = (aDecoder.decodeObject(forKey: "acMinTemperature") as! Int )
acMaxTemperature = (aDecoder.decodeObject(forKey: "acMaxTemperature") as! Int )
weatherTemperature = (aDecoder.decodeObject(forKey: "weatherTemperature") as! Int)
roomTemperature = (aDecoder.decodeObject(forKey: "roomTemperature") as! Int )
currentAcMode = (aDecoder.decodeObject(forKey: "currentAcMode") as! AcModeType)
currentFanMode = (aDecoder.decodeObject(forKey: "currentFanMode") as! FanModeType)
lastPowerOnTime = (aDecoder.decodeObject(forKey: "lastPowerOnTime") as! Double)
if let timeTable = aDecoder.decodeObject(forKey: "timeTable") as! [TimeTable]? {self.timeTable=timeTable}
}

public override func encode(with aCoder: NSCoder) {
aCoder.encode(self.brandId, forKey: "brandId")
aCoder.encode(self.variant, forKey: "variant")
aCoder.encode(self.brandName, forKey: "brandName")
aCoder.encode(self.acTemperature, forKey: "temperature")
aCoder.encode(self.timeTable, forKey: "timeTable")
aCoder.encode(self.acMinTemperature, forKey: "acMinTemperature")
aCoder.encode(self.acMaxTemperature, forKey: "acMaxTemperature")
aCoder.encode(self.weatherTemperature, forKey: "weatherTemperature")
aCoder.encode(self.roomTemperature, forKey: "roomTemperature")
aCoder.encode(self.currentAcMode, forKey: "currentAcMode")
aCoder.encode(self.currentFanMode, forKey: "currentFanMode")
aCoder.encode(self.lastPowerOnTime, forKey: "lastPowerOnTime")
}

required public init() {
super.init();
brandId = ""
variant = ""
brandName = ""

isPowerOn = false;
isSleepOn = false;
isTimeTableOn = false;
hasScreen = false;
currentAcMode = .acModeCool
acMinTemperature = 16;
acMaxTemperature = 32;
currentFanMode = .fanModeLow
lastPowerOnTime = -1;
timeTable = [TimeTable]()
}
}

最佳答案

在您的 required public init?(coder aDecoder: NSCoder) 中,您正在使用 as! 强制转换 decodeObject 的结果,因此,如果失败,这将使您的应用程序崩溃。相反,您可以使用 as? 结合默认值来解包您的可选值。

例如:

brandId = aDecoder.decodeObject(forKey: "brandId") as? String ?? "your default string"

这使用 ??,它是 nil 合并运算符,当左侧为 nil 时提供默认值。

关于swift - 尝试获取 nil 值时解码器崩溃也编码解码枚举值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49671869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com