0

Hi everyone I'm new to SwiftUI and i try to make a simple app for random choices,So when you create a list choices that will turn to a Spin-WheelView.For now, I face two problems: First. I don't know how save Angle and Array to UserDefaults; Second: I don't know to connect choices view to spin-wheel view. Any helps i will appreciate it! thank you all.

        import Foundation
    import SwiftUI
    
    struct ChooseModel: Identifiable, Codable {
        var id = UUID()
        var decision: String
        var choices: [String]
        // i try to create Angle and [Colors] in my model,
        // but they don't confirmed Codable, i don't know how to save them to User defaults.
    //    var startAngle: Angle
    //    var endAngle: Angle
    //    var colors: [Color]
    }
    
        import Foundation
    import SwiftUI
    
    class ChooseModelView: ObservableObject {
    
        @Published var models = [ChooseModel]() {
            didSet {
                let encoder = JSONEncoder()
                if let encoded = try? encoder.encode(models) {
                    UserDefaults.standard.set(encoded, forKey: "models")
                }
            }
        }
        init() {
            if let items = UserDefaults.standard.data(forKey: "models") {
                let decoder = JSONDecoder()
                if let decoded = try? decoder.decode([ChooseModel].self, from: items) {
                    self.models = decoded
                    return
                }
            }
            self.models = []
        }
    }
    
        import SwiftUI
    
    
    struct ChooseView: View {
        
        @ObservedObject var chooseMV = ChooseModelView()
        @State private var showingAddView = false
        
        var body: some View {
            NavigationView {
                List {
                    ForEach(chooseMV.models) { item in
                        // Use NavigationLink to Each WheelView
                        HStack {
                            VStack(alignment: .leading) {
                                Text(item.decision)
                                    .font(.title3)
                                HStack {
                                    ForEach(item.choices, id: \.self) { choice in
                                        Text("\(choice),")
                                            .foregroundColor(Color.green)
                                    }
                                }
                            }
                            Spacer()
                        }
                        
                    }.onDelete(perform: remove)
                }.navigationTitle("iChoose")
                    .navigationBarItems(leading: EditButton(), trailing: Button(action: {
                        self.showingAddView = true
                    }, label: {
                        Image(systemName: "plus")
                    }))
                    .sheet(isPresented: $showingAddView) {
                        AddView(chooseMV: self.chooseMV)
                    }
            }
        }
        func remove(at offsets: IndexSet) {
            chooseMV.models.remove(atOffsets: offsets)
        }
    }

My View

Conect to spin-wheel view

tankman
  • 13
  • 1
  • 3
  • You can't do that because both `Angle` and `Color` are not in conformance with `Codable` protocol. First, check [this answer](https://stackoverflow.com/questions/34366171/how-do-i-save-a-uicolor-with-userdefaults) for color and about angle, you can store it as CGFloat instead, then use a computed property to obtain it as Angle. – Jan Cássio Dec 03 '21 at 21:03

1 Answers1

0

You might want to take a look at Codable, but basically your struct needs to have every one of it's members conforming to codable, so that it can automatically conform to Codable. If any of it's members are not codable, you need to encode/decode your struct manually. You currently have two types that are not Codable: Angle and UIColor. Since Angle is a custom type, you need to make it conform to Codable or change it to an already Codable type, like Double or CGFloat. UIColor is a bit trickier, and you might want to check this answer on how to do it

Pastre
  • 684
  • 8
  • 17
  • Thank you sir, I tried use property wrapper make uicolor codable but i still stuck on make custom type to confirm to codable. – tankman Dec 05 '21 at 19:49