In the customizationID modifier of the TabView, if I use an ID with a SwiftData value, it returns the following error when I open a new window on iPadOS: “Not unique” even when using a UUID. I can't do anything about it, so I'm posting it here. Any advice that could help me on the way to solving this problem would be appreciated.
Thread 1: "Fatal: supplied item identifiers are not unique. Duplicate identifiers: {(\n "Tab.Custom.1C350509-C28A-4C41-85A0-3EA57779DB1B"\n)}"
Below is a portion of the code that reproduces this phenomenon. I believe it is sufficient for your needs, but if you need more code, we can accommodate.
struct ContentView: View {
@Environment(\.modelContext) var modelContext
@Query private var tags: [Tag]
@AppStorage("Customization") private var customization: TabViewCustomization
@State private var isAdding = false
@State private var tagName = ""
var body: some View {
TabView {
Tab("Home", systemImage: "house") {
NavigationStack {
Label("Home", systemImage: "house")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button {
isAdding.toggle()
} label: {
Label("Add Tag", systemImage: "plus")
}
}
}
}
}
.customizationID("Tab.Home")
Tab("Workplace", systemImage: "building.2") {
NavigationStack {
Label("Workplace", systemImage: "building.2")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button {
isAdding.toggle()
} label: {
Label("Add Tag", systemImage: "plus")
}
}
}
}
}
.customizationID("Tab.Workplace")
Tab("Apple Store", systemImage: "apple.logo") {
NavigationStack {
Label("Apple Store", systemImage: "apple.logo")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button {
isAdding.toggle()
} label: {
Label("Add Tag", systemImage: "plus")
}
}
}
}
}
.customizationID("Tab.AppleStore")
TabSection {
ForEach(tags) { tag in
Tab(tag.name, systemImage: "tag") {
NavigationStack {
Label(tag.name, systemImage: "tag")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button {
isAdding.toggle()
} label: {
Label("Add Tag", systemImage: "plus")
}
}
}
}
}
.customizationID("Tab.Custom.\(tag.id.uuidString)")
}
} header: {
Label("Custom", systemImage: "tag")
}
.customizationID("TabSection.AppleStore")
.sectionActions {
Button {
isAdding.toggle()
} label: {
Label("Add Tag", systemImage: "plus")
}
}
}
.tabViewCustomization($customization)
.tabViewStyle(.sidebarAdaptable)
.alert("Create Tag", isPresented: $isAdding) {
TextField("Tag Name", text: $tagName)
Button("Cancel") {
tagName = ""
isAdding = false
}
Button("Done") {
let tagList = tagName.components(separatedBy: .whitespaces)
for tagItem in tagList {
if tagItem != "" && tags.filter({return $0.name == tagItem}).isEmpty {
let newTag = Tag(name: tagItem)
modelContext.insert(newTag)
}
}
try? modelContext.save()
tagName = ""
isAdding = false
}
}
}
}
import SwiftData
import Foundation
@Model
final class Tag: Identifiable {
var id: UUID = UUID()
var name: String = ""
init(name: String) {
self.id = UUID()
self.name = name
}
}
How to reproduce
- Implement the above code so that it works.
- Build the application on the actual iPad device using Xcode.
- Add tags (data)
- Open a new window and check the UI behavior.
Correct Behavior
The ID is not actually unique. Or, if it is unique, it should not freeze without generating an error.
Actual Behavior
The ID should be set to be unique, but it freezes with an error that it is not unique.
Environment
iPadOS 18 RC (22A3354)
Xcode 16.0 (16A242)