Broken func value(for component: Calendar.Component) -> Int?

Is there a workaround for value(for component: Calendar.Component) seemingly being broken? Or a dynamic way to call the property version?


func dateComponentsValueShouldBeNil() { 
    let dateComponents = DateComponents(month: 3) 
    debugPrint("Month", dateComponents.month) // "Month" Optional(3)
    debugPrint("Property", dateComponents.hour) // "Property" nil
    debugPrint("Enumeration", dateComponents.value(for: .hour)) // "Enumeration" Optional(9223372036854775807)
}

Replies

// "Enumeration" Optional(9223372036854775807)

This value is

NSNotFound
, which is what you’d get when if you called the Objective-C API. You should be able to write a wrapper that looks for that value and returns nil instead.

Also, please make sure you file a bug about this, and post the bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"