Skip to content

Instantly share code, notes, and snippets.

@daltonclaybrook
Created September 4, 2023 16:09
Show Gist options
  • Save daltonclaybrook/5491f296d9d5806dd31f4d777aea347f to your computer and use it in GitHub Desktop.
Save daltonclaybrook/5491f296d9d5806dd31f4d777aea347f to your computer and use it in GitHub Desktop.
An example unwrapping a (Date, Text.DateStyle) from LocalizedStringKey underlying storage. I'm not sure how to actually format the date with the DateStyle.
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
private func stringFromDateStyle(storage: Any) -> String? {
let dateStyleChildren = Array(Mirror(reflecting: storage).children)
guard dateStyleChildren.count == 2, let text = dateStyleChildren[0].value as? Text else {
return nil
}
let textStorage = Array(Mirror(reflecting: text).children)[0].value // Text.Storage
let dateTextStorage = Array(Mirror(reflecting: textStorage).children)[0].value
guard "\(type(of: dateTextStorage))" == "DateTextStorage" else {
return nil
}
guard let (date, dateStyle) = Array(Mirror(reflecting: Array(Mirror(reflecting: dateTextStorage).children)[0].value).children)[0].value as? (Date, Text.DateStyle) else {
return nil
}
return "???"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment