I’m very new to SwiftData and I’ve been confused for this subject. I searched nearly all of the posts/ movies /Udemy programs however the instances are all the time completely different from mine. I attempted Question/Fetch and likewise with GPT however nonetheless no solutions but
I’m making a PDF reader, for every web page enable consumer to make drawings (with a canvas above the web page). The issue is within the canvasView, I can’t fetch the particular canvas information for sure web page. (pdfFile and Web page I saved in an environmentObject).
Please advise
struct CanvasView: UIViewRepresentable {
@EnvironmentObject var thisContentModel: envContentModel
@Setting(.modelContext) personal var modelContext: ModelContext
//var currentFile: ContentModel
//var currentPage: Int
@Binding var pdfPageBounds: CGRect
@Binding var pdfViewScale: CGFloat
@Question personal var pdfDrawingList: [PDFDrawing]
@State personal var drawing = PKDrawing()
var thisPageDrawing: PDFDrawing {
if let existedDrawing = pdfDrawingList.first(the place: { $0.file == thisContentModel.contentModel && $0.web page == thisContentModel.currentPage }) {
return existedDrawing
} else {
let newDrawing = PDFDrawing(web page: thisContentModel.currentPage, drawingData: drawing.dataRepresentation(), file: thisContentModel.contentModel)
modelContext.insert(newDrawing)
return newDrawing
}
}
func makeUIView(context: Context) -> PKCanvasView {
let canvasView = PKCanvasView()
canvasView.drawingPolicy = .anyInput
canvasView.delegate = context.coordinator
canvasView.device = PKInkingTool(.pen, coloration: .black, width: 5)
canvasView.backgroundColor = .clear
canvasView.isOpaque = false
canvasView.becomeFirstResponder()
// 将 drawingData 转换为 PKDrawing
if let drawingFromData = attempt? PKDrawing(information: thisPageDrawing.drawingData) {
canvasView.drawing = drawingFromData
} else {
canvasView.drawing = PKDrawing()
}
return canvasView
}
The PDFDrawings information mannequin as under:
@Mannequin
class PDFDrawing: ObservableObject {
//@Attribute var fileID: UUID
@Attribute var web page: Int
@Attribute var drawingData: Knowledge
@Relationship
var file: ContentModel
init(web page: Int, drawingData: Knowledge, file: ContentModel) {
self.web page = web page
self.drawingData = drawingData
self.file = file
//self.fileID = file.id
}
}