CoreData 프레임 워크를 사용하여 앱을 시작할 때 항상이 런타임 오류가 발생합니다. FetchRequest 설정 지침을 정확히 따랐지만이 오류가 계속 발생합니다.
Could not cast value of type 'Swift.Optional<Swift.AnyObject>' (0x7feb68884da0)
to 'NSFetchRequest' (0x7feb68884e40).
여기서 사용하고 싶은 뷰에서 FetchRequest를 설정합니다.
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(fetchRequest: NotificationsItem.getAllNotificationsItems()) var notificationsItems:FetchedResults<NotificationsItem>
NotificationsItem 클래스에서 설정하는 방법은 다음과 같습니다.
import CoreData
import Foundation
public class NotificationsItem: NSManagedObject, Identifiable {
@NSManaged public var createdAt: Date?
@NSManaged public var type: String?
}
extension NotificationsItem {
static func getAllNotificationsItems() -> NSFetchRequest<NotificationsItem> {
let request:NSFetchRequest<NotificationsItem> = NotificationsItem.fetchRequest() as! NSFetchRequest<NotificationsItem>
let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)
request.sortDescriptors = [sortDescriptor]
return request
}
}
이 코드가 강사 사본에서 완벽하게 작동하더라도이 오류가 계속 발생하는 이유는 무엇입니까?
NSManagedObject 클래스 정의 맨 위에 추가하십시오.
//This need to be declared here
@objc(NotificationsItem)
public class NotificationsItem: NSManagedObject, Identifiable {
...
그리고 클래스의 Codegen을 Manual/None
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다