如何在Swift Xcode中编码NSSet?
我知道如何在解码时做到这一点,如下所述,但如何进行编码呢 ? 只需查看weatherDetail:NSSet我删除了其他容器,因为这对于示例来说是不必要的 在这里,我做错了,但我想知道我如何才能弄明白这一点如何在Swift Xcode中编码NSSet?,swift,xcode,core-data,model,encodable,Swift,Xcode,Core Data,Model,Encodable,我知道如何在解码时做到这一点,如下所述,但如何进行编码呢 ? 只需查看weatherDetail:NSSet我删除了其他容器,因为这对于示例来说是不必要的 在这里,我做错了,但我想知道我如何才能弄明白这一点 我找到了一个解决办法,其实很简单。 您需要做的是: 将NSSet转换为WeatherDetail数组 对数组进行编码 在您的示例中,这将是: var container = encoder.container(keyedBy: CodingKeys.self) let weatherDeta
我找到了一个解决办法,其实很简单。 您需要做的是:
var container = encoder.container(keyedBy: CodingKeys.self)
let weatherDetailArray = self.weatherDetail.allObjects as? [WeatherDetail]
try container.encode(weatherDetailArray, forKey: . weatherDetail)
你找到解决办法了吗?
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
// NSSet(array: try container.encode(weatherDetail.self, forKey: .weatherDetail)) }
var container = encoder.container(keyedBy: CodingKeys.self)
let weatherDetailArray = self.weatherDetail.allObjects as? [WeatherDetail]
try container.encode(weatherDetailArray, forKey: . weatherDetail)