Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Swift Xcode中编码NSSet?_Swift_Xcode_Core Data_Model_Encodable - Fatal编程技术网

如何在Swift Xcode中编码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

我知道如何在解码时做到这一点,如下所述,但如何进行编码呢 ?

只需查看weatherDetail:NSSet我删除了其他容器,因为这对于示例来说是不必要的

在这里,我做错了,但我想知道我如何才能弄明白这一点


我找到了一个解决办法,其实很简单。 您需要做的是:

  • 将NSSet转换为WeatherDetail数组
  • 对数组进行编码
  • 在您的示例中,这将是:

    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)