You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In that converter I added conformance to NSSecureCoding similarly as UnitConverterLinear does.
During attempt to serialize such a measurement with NSKeyedArchiver I get a runtime exception EXC_BAD_INSTRUCTION:
Upon further investigation I noticed that Swift implementation of Measurement struct explicitly checks for converter class and if it's not UnitConverterLinear, it triggers a precondition failure. Considering that almost all types of Measurements and Units framework conform to NSSecureCoding protocol maybe it is reasonable to add such a conformance to UnitConverter base class and then encode and decode it with NSKeyed(Un)archiver like this:
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0,*)extensionMeasurement:Codable{privateenumCodingKeys:Int,CodingKey{case value
case unit
}privateenumUnitCodingKeys:Int,CodingKey{case symbol
case converter
}publicinit(from decoder:Decoder)throws{letcontainer=try decoder.container(keyedBy:CodingKeys.self)letvalue=try container.decode(Double.self, forKey:.value)letunitContainer=try container.nestedContainer(keyedBy:UnitCodingKeys.self, forKey:.unit)letsymbol=try unitContainer.decode(String.self, forKey:.symbol)letunit:UnitType
if UnitType.self is Dimension.Type{letconverterData=try unitContainer.decode(Data.self, forKey:.converter)letconverter=tryNSKeyedUnarchiver.unarchiveTopLevelObjectWithData(converterData)as?UnitConverterletunitMetaType=(UnitType.self as!Dimension.Type)
if let converter = converter {
unit =(unitMetaType.init(symbol: symbol, converter: converter)as!UnitType)}else{
unit =UnitType(symbol: symbol)}}else{
unit =UnitType(symbol: symbol)}self.init(value: value, unit: unit)}publicfunc encode(to encoder:Encoder)throws{varcontainer= encoder.container(keyedBy:CodingKeys.self)try container.encode(self.value, forKey:.value)varunitContainer= container.nestedContainer(keyedBy:UnitCodingKeys.self, forKey:.unit)try unitContainer.encode(self.unit.symbol, forKey:.symbol)
if UnitType.self is Dimension.Type{letconverter=(self.unit as!Dimension).converter
letconverterData=tryNSKeyedArchiver.archivedData(withRootObject: converter, requiringSecureCoding: false)try unitContainer.encode(converterData, forKey:.converter)}}}
Environment
Swift 5.3 (swiftlang-1200.0.29.2 clang-1200.0.30.1)
Xcode 12.0 GM
Additional Detail from JIRA
md5: 39638118a3a39937e75933935b5cef3d
Issue Description:
In my project I extended a
UnitAngle
with a custom unit that relies onUnitConverter
subclass:In that converter I added conformance to
NSSecureCoding
similarly asUnitConverterLinear
does.During attempt to serialize such a measurement with
NSKeyedArchiver
I get a runtime exceptionEXC_BAD_INSTRUCTION
:The text was updated successfully, but these errors were encountered: