Skip to content

[SR-13784] Exception during Measurement serialization with a Unit that uses custom UnitConverter #3966

@swift-ci

Description

@swift-ci
Previous ID SR-13784
Radar rdar://problem/70730985
Original Reporter Rostyslav_D (JIRA User)
Type Bug
Environment

Swift 5.3 (swiftlang-1200.0.29.2 clang-1200.0.30.1)
Xcode 12.0 GM

Additional Detail from JIRA
Votes 0
Component/s Foundation, Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 39638118a3a39937e75933935b5cef3d

Issue Description:

In my project I extended a UnitAngle with a custom unit that relies on UnitConverter subclass:

extension UnitAngle {
   static let cosine = UnitAngle(symbol: "cosφ", converter: CosineUnitConverter())
}

class CosineUnitConverter: UnitConverter, NSSecureCoding, Codable {
    static var supportsSecureCoding: Bool { true }
    required convenience init?(coder: NSCoder) {
         self.init()
    }
    func encode(with coder: NSCoder) { }
    override func baseUnitValue(fromValue value: Double) -> Double {
         let rad = acos(value)
         return rad / .pi * 180
    }
    override func value(fromBaseUnitValue baseUnitValue: Double) -> Double {
         let rad = baseUnitValue * .pi / 180
         return cos(rad)
    }
}

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:

let archiver = NSKeyedArchiver(requiringSecureCoding: false)
let measurement = Measurement<UnitAngle>(value: 1, unit: .cosine)
try archiver.encodeEncodable(measurement, forKey: NSKeyedArchiveRootObjectKey)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions