UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Can someone explain my mistake here.

Forums > SwiftUI

@onqun  

Hello everyone, I am trying to save data into phone, I got this error.

Type 'Item' does not conform to protocol 'Encodable/Decodable'

Later I googled and modified my code with enumeration, without understanding it. Fortunetly I still get the same error. Can someone explain to me how can I make it work?

import SwiftUI

class ArrayofItems: ObservableObject, Identifiable, Codable {

    var items: [Item] = []

    func addnewItem(itemName: String, itemCount: Int){
        items.append(Item(itemName: itemName, itemCount: itemCount))
    }

}

class Item: ObservableObject, Identifiable, Codable  {

    enum CodingKeys: CodingKey {
        case itemName
        case id
        case itemCount
    }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        itemName = try container.decode(String.self, forKey: .itemName)
        itemCount = try container.decode(Int.self, forKey: .itemCount)
    }

   @Published var id = UUID()
   @Published var itemName: String
   @Published var itemCount: Int

    init(itemName: String, itemCount: Int) {
        self.itemName = itemName
        self.itemCount = itemCount
    }

    func add() {
        itemCount += 1
    }

    func sub() {
        itemCount -= 1
    }

}

extension Item {
    static var sample: [Item] {
        [
            Item(itemName: "Item1", itemCount: 3),
            Item(itemName: "Item2", itemCount: 1),
            Item(itemName: "Item3", itemCount: 2)
        ]
    }
}
import Foundation

@MainActor

class allofItems: ObservableObject {

    @Published var allItems: [ArrayofItems] = []

    private static func fileURL() throws -> URL {
        try FileManager.default.url(for: .documentDirectory,
                                    in: .userDomainMask,
                                    appropriateFor: nil,
                                    create: false)
        .appendingPathComponent("item.data")
    }

    func load() async throws {
        let item = Task<[ArrayofItems], Error> {
            let fileURL = try Self.fileURL()
            guard let data = try? Data(contentsOf: fileURL) else {
                return []
            }
            let myitems = try JSONDecoder().decode([ArrayofItems].self, from: data)
            return myitems
        }

        let allItems = try await item.value
        self.allItems = allItems
    }

    func save(allItems: [ArrayofItems]) async throws {
            let task = Task {
                let data = try JSONEncoder().encode(allItems)
                let outfile = try Self.fileURL()
                try data.write(to: outfile)
            }
            _ = try await task.value
        }

}

2      

You made the Item class conform to Decodable with your required init function. But to conform to Codable the class must conform to both Decodable and Encodable. You have not written the code to conform to Encodable.

To conform to Encodable, add an encode function to the Item class. Get the encoder's container. Call the container's encode method for each property in the class.

func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(name, forKey: .itemName)
    try container.encode(name, forKey: .itemCount)
}

See the following article for more details:

https://www.hackingwithswift.com/books/ios-swiftui/adding-codable-conformance-for-published-properties

3      

You're getting a "Type 'Item' does not conform to protocol 'Encodable/Decodable'" error because you need to add an encode(to encoder: Encoder) method in your Item class. This method defines how to encode your Item objects for saving or transmission. Add this method to resolve the error.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.