URLHintArray.swift 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // URLHintArray.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 3/1/26.
  6. //
  7. import Foundation
  8. import SwiftData
  9. internal import Combine
  10. typealias URLHintArray = [URLHint]
  11. /**
  12. * @brief Provides a UserDefaults-compatibiliy layer for Array<URLHint> that
  13. * allows seamless reading/writing on program startup and changes.
  14. * We bind this type to its JSON-String representation rather than, say, an
  15. * Array of dictionaries because @AppStorage only operates on scalar types.
  16. *
  17. * @author Sam Jaffe
  18. * @author KD Knowledge Diet https://paigeshin1991.medium.com/saving-custom-codable-types-in-appstorage-or-scenestorage-in-swiftui-0073032f3f94
  19. *
  20. * The \@retroactive guards against the Swift developers changing Array<T> to
  21. * comply w/ RawRepresentable in the future
  22. */
  23. extension URLHintArray : @retroactive RawRepresentable {
  24. public init?(rawValue: String) {
  25. guard let data = rawValue.data(using: .utf8),
  26. let result = try? JSONDecoder().decode(URLHintArray.self, from: data)
  27. else {
  28. return nil
  29. }
  30. self = result
  31. }
  32. public var rawValue: String {
  33. guard let data = try? JSONEncoder().encode(self),
  34. let result = String(data: data, encoding: .utf8)
  35. else {
  36. return "[]"
  37. }
  38. return result
  39. }
  40. }