History
Loading...
Loading...
September 11, 2025
.dropDestination(for:) with UTType for type-safe drag and drop operations. This modifier automatically handles validation and provides clean APIs for file drops: .dropDestination(for: URL.self) { urls, location in /* handle */ }This practice validates dropped files against specific UTTypes before processing, ensuring only supported file types are accepted while providing immediate user feedback through the return boolean.
struct DocumentDropZone: View {
@State private var droppedFiles: [URL] = []
var body: some View {
RoundedRectangle(cornerRadius: 12)
.fill(Color.gray.opacity(0.2))
.frame(height: 200)
.overlay {
VStack {
Image(systemName: "doc.badge.plus")
Text("Drop PDF or text files here")
}
}
.dropDestination(for: URL.self) { urls, location in
let validFiles = urls.filter { url in
url.pathExtension.lowercased() == "pdf" ||
url.pathExtension.lowercased() == "txt"
}
droppedFiles.append(contentsOf: validFiles)
return !validFiles.isEmpty
}
}
}Type-safe validation prevents runtime errors from unsupported files, improves user experience with clear visual feedback, and maintains app stability by filtering content at the UI boundary rather than during processing.