So I’ve got the following code, which seems to work, and I’m wondering if there is a better, cleaner way to approach adding/editing elements in an array.
var category: Category
var idx: Int = -1
switch mode {
case .add:
category = Category()
case .edit(let _category):
category = _category
idx = categoryViewModel.categories.firstIndex(of: _category) ?? idx
}
category.name = categoryName
category.icon = "category-\(categoryIdx)"
category.color = colors[colorIdx]
switch mode {
case .add:
categoryViewModel.categories.append(category)
case .edit:
categoryViewModel.categories[idx] = category
}
I understand I’m not checking idx
to make sure it’s not -1
. I’m not concerned about that part right now. It’s the overall approach I’m looking for thoughts on.
Thanks!
You must log in or register to comment.