swiftUI 尝试构建一个笔记类的app,用户可以导入pdf,然后在pdf添加笔记,但是现在遇到一个编译报错如下:
== PREVIEW UPDATE ERROR:
SchemeBuildError: Failed to build the scheme “AIreader”
'Element' is not a member type of generic enum '[AIreader.ContentModel]?'
Emitting module for AIreader:
/var/folders/wb/hw9lz_yj2cl78nrb0tvfrh1w0000gn/T/swift-generated-sources/@__swiftmacro_8AIreader11ContentViewV12contentModel33_48E5B9AF3FB619EEEC334395AF9ABA5CLL5QueryfMp_.swift:1:66: error: 'Element' is not a member type of generic enum '[AIreader.ContentModel]?'
private (set) var _contentModel: SwiftData.Query<[ContentModel]?.Element, [ContentModel]?> = .init()
~~~~~~~~~~~~~~~ ^
/Users/xx/Library/Mobile Documents/com~apple~CloudDocs/app_AiReader/AIreader/AIreader/Views/ContentView.swift:16:5: note: in expansion of macro 'Query' on property 'contentModel' here
@Query private var contentModel: [ContentModel]?
^ ~~~~~~~~~~~~
Swift.Optional:1:21: note: 'Optional' declared here
@frozen public enum Optional<Wrapped> : ExpressibleByNilLiteral {
^
这是报错代码:
struct ContentView: View {
let fileURL: URL
let onClose: () -> Void
@State private var isDrawing: Bool = false
@State private var currentPage: Int = 0 // 当前页码
@State private var currentNotes: [Note] = []
@State private var currentCards: [Card] = []
@State private var currentOutlines: [Outline] = []
@Environment(\.modelContext) private var modelContext: ModelContext
@Query private var contentModel: [ContentModel]?
init(fileURL: URL, onClose: @escaping () -> Void) {
self.fileURL = fileURL
self.onClose = onClose
print("contentview iunit")
_contentModel = Query(filter: #Predicate<ContentModel> { $0.fileName == fileURL.lastPathComponent })
}
报错位置在query这里:
model结构如下,供参考
@Model
final class ContentModel {
@Attribute(.unique) var fileName: String
var currentPage: Int?
var outlineList: [Outline]?
var noteList: [Note]?
var cardList: [Card]?
init(fileName: String,
currentPage: Int? = nil,
outlineList: [Outline]? = nil,
noteList: [Note]? = nil,
cardList: [Card]? = nil) {
self.fileName = fileName
self.currentPage = currentPage
self.noteList = noteList
self.outlineList = outlineList
self.cardList = cardList
}
}