| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- //
- // CodableArray.swift
- // Todos
- //
- // Created by Sam Jaffe on 3/1/26.
- //
- import Foundation
- import SwiftData
- internal import Combine
- typealias CodableArray<A: Codable> = [A]
- // swiftlint:disable line_length
- /**
- * @brief Provides a UserDefaults-compatibiliy layer for Array<URLHint> that
- * allows seamless reading/writing on program startup and changes.
- * We bind this type to its JSON-String representation rather than, say, an
- * Array of dictionaries because @AppStorage only operates on scalar types.
- *
- * @author Sam Jaffe
- * @author KD Knowledge Diet https://paigeshin1991.medium.com/saving-custom-codable-types-in-appstorage-or-scenestorage-in-swiftui-0073032f3f94
- *
- * The \@retroactive guards against the Swift developers changing Array<T> to
- * comply w/ RawRepresentable in the future
- */
- extension CodableArray: @retroactive RawRepresentable {
- // swiftlint:enable line_length
- public init?(rawValue: String) {
- guard let data = rawValue.data(using: .utf8),
- let result = try? JSONDecoder().decode(CodableArray.self, from: data)
- else {
- return nil
- }
- self = result
- }
- public var rawValue: String {
- guard let data = try? JSONEncoder().encode(self),
- let result = String(data: data, encoding: .utf8)
- else {
- return "[]"
- }
- return result
- }
- }
|