Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project 7 - UUID id property #108

Open
sdavids opened this issue Jul 16, 2024 · 1 comment
Open

Project 7 - UUID id property #108

sdavids opened this issue Jul 16, 2024 · 1 comment

Comments

@sdavids
Copy link

sdavids commented Jul 16, 2024

Hi,

you might want to change your chosen fix for the "Immutable property will not be decoded because it is declared with an initial value which cannot be overwritten" problem with the UUID id property and the Codable conformance.

struct ExpenseItem: Identifiable, Codable {
  let id = UUID()
  let name: String
  let type: String
  let amount: Double
}

struct ExpenseItem: Identifiable, Codable {
  private(set) var id = UUID()
  let name: String
  let type: String
  let amount: Double
}

instead of

struct ExpenseItem: Identifiable, Codable {
  var id = UUID()
  let name: String
  let type: String
  let amount: Double
}

The current fix allows item.id = UUID()—breaking the concept of having a stable unique ID:

Button("💣") {
  let same = UUID()
  for i in expenses.items.indices {
    expenses.items[i].id = same
  }
}

List {
  ForEach(expenses.items) { item in
    // ... depends on Identifiable and stable unique IDs
  }
}

Whereas the proposal would prohibit it but still allow struct initialization and decoding.

@Brai-hit
Copy link

Excellent 🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants