Swift: How to add more than one item to JSON array?

僤鯓⒐⒋嵵緔 提交于 2020-05-17 06:23:27

问题


I am trying to create a basic to-do application for command-line in Swift. Below is my function to add a new item to the to-do array, but the new entry keeps overwriting the old one, instead of creating a new one. In the end, there is only one entry in the todo.json file.

When I multiply the entries and .append statements manually it works, but probably my brain is too dead to figure it out at the moment.


struct TodoItem: Codable {
    let name: String
}

var todoList = [TodoItem]()

func addToList(_ item: String) -> String {
    let todoItem = TodoItem(name: item)
    todoList.append(todoItem)

    do {
        let fileURL = try FileManager.default
            .url(for: .applicationSupportDirectory,
                 in: .userDomainMask,
                 appropriateFor: nil,
                 create: true)
             .appendingPathComponent("example")
             .appendingPathExtension("json")

        let encoder = JSONEncoder()
        try encoder.encode(todoList).write(to: fileURL)

    } catch {
        return "Error: \(error.localizedDescription)"
    }
        return "Item added: \(todoItem.name)"
}

回答1:


Your code works fine. I think the problem rests in the fact that todoList is empty when you run this. But you can write code to retrieve the contents of the JSON:

var todoList: [TodoItem] = []

func retrieveToDoList() {
    guard let data = try? Data(contentsOf: fileURL) else { return }

    todoList = (try? JSONDecoder().decode([TodoItem].self, from: data)) ?? []
}

So, for example, consider:

retrieveToDoList()

addToList("foo")
addToList("bar")
addToList("baz")

print(todoList)

// if you want to check the json

let data = try! Data(contentsOf: fileURL)
let json = String(data: data, encoding: .utf8)!
print(json)

That results in:

[MyApp.TodoItem(name: "foo"), MyApp.TodoItem(name: "bar"), MyApp.TodoItem(name: "baz")]
[
  {
    "name" : "foo"
  },
  {
    "name" : "bar"
  },
  {
    "name" : "baz"
  }
]

And if I later do:

addToList("abc")
addToList("def")
addToList("hij")

I then get:

[
  {
    "name" : "foo"
  },
  {
    "name" : "bar"
  },
  {
    "name" : "baz"
  },
  {
    "name" : "abc"
  },
  {
    "name" : "def"
  },
  {
    "name" : "hij"
  }
]

So, every time the app starts up, just make sure to call retrieveToDoList before trying to append items or else the toDoList will be empty.


FYI, this is the code I used to generate the above. Hopefully it illustrates the idea.

struct TodoItem: Codable {
    let name: String
}

class ViewController: UIViewController {
    private var todoList: [TodoItem] = []

    private let fileURL = try! FileManager.default
        .url(for: .applicationSupportDirectory,
             in: .userDomainMask,
             appropriateFor: nil,
             create: true)
        .appendingPathComponent("example.json")

    override func viewDidLoad() {
        super.viewDidLoad()

        retrieveToDoList()

        addToList("foo")
        addToList("bar")
        addToList("baz")

        print(todoList)

        // if you want to check the json

        let data = try! Data(contentsOf: fileURL)
        let json = String(data: data, encoding: .utf8)!
        print(json)
    }
}

private extension ViewController {
    func retrieveToDoList() {
        guard let data = try? Data(contentsOf: fileURL) else { return }

        todoList = (try? JSONDecoder().decode([TodoItem].self, from: data)) ?? []
    }

    @discardableResult
    func addToList(_ item: String) -> String {
        let todoItem = TodoItem(name: item)
        todoList.append(todoItem)

        do {
            let encoder = JSONEncoder()
            encoder.outputFormatting = .prettyPrinted
            try encoder.encode(todoList).write(to: fileURL)
        } catch {
            return "Error: \(error.localizedDescription)"
        }

        return "Item added: \(todoItem.name)"
    }
}


来源:https://stackoverflow.com/questions/60745982/swift-how-to-add-more-than-one-item-to-json-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!