I’m attempting to make an inventory of all TestItem objects however I get this error:
'NSInvalidArgumentException', purpose: '-[TestItem copyWithZone:]: unrecognized selector despatched to occasion.
Information mannequin
TestItemList+CoreDataProperties
import Basis
import CoreData
extension TestItemList {
@nonobjc public class func fetchRequest() -> NSFetchRequest<TestItemList> {
return NSFetchRequest<TestItemList>(entityName: "TestItemList")
}
@NSManaged public var title: String?
@NSManaged public var testitems: NSSet?
public var itemArray: [TestItem] {
let set = testitems as? Set<TestItem> ?? []
return set.sorted{
$0.title! < $1.title!
}
}
}
extension TestItemList : Identifiable {
}
ContentView
import SwiftUI
import CoreData
struct ContentView: View {
@Atmosphere(.managedObjectContext) personal var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: TestItemList.name, ascending: true)],
animation: .default)
personal var itemLists: FetchedResults<TestItemList>
@State var listExists: Bool = false
var physique: some View {
NavigationView {
VStack{
Record{
ForEach(itemLists, id: .self){ Record in
NavigationLink(vacation spot: ListElementsView(record: Record)){
Textual content("(Record.title!)")
}
}.onDelete(carry out: deleteItemList)
}
if(listExists){
Textual content("Record exists!").foregroundColor(.pink)
}
Button("Add at the moment's record", motion: {
addItemList()
}).disabled(listExists).buttonStyle(.borderedProminent)
Record{
ForEach(testitems){
merchandise in Textual content("(merchandise.title!), (merchandise.record!.title!)")
}
}
}
}
}
personal func addItemList() {
let dateFormatter : DateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = Date()
let dateString = dateFormatter.string(from: date)
if(!itemLists.comprises(the place: {$0.title == dateString})){
let newList = TestItemList(context: viewContext)
newList.title = dateString
do{
attempt viewContext.save()
} catch {
let error = error as NSError
fatalError("Error (error), (error.userInfo)")
}
}else{
listExists = true
}
}
personal func deleteItemList(offsets: IndexSet) {
withAnimation {
offsets.map { itemLists[$0] }.forEach(viewContext.delete)
do {
attempt viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error (nsError), (nsError.userInfo)")
}
}
}
}
personal let itemFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .brief
formatter.timeStyle = .medium
return formatter
}()
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().surroundings(.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}
ListElementsView
import SwiftUI
struct ListElementsView: View {
@Atmosphere (.managedObjectContext) personal var viewContext
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: TestItem.name, ascending: true)], animation: .default)
personal var testitems: FetchedResults<TestItem>
var record: TestItemList
@State personal var textual content: String = ""
var inputNotOk: Bool{
textual content.isEmpty
}
var physique: some View {
VStack{
Textual content(record.title!).font(.largeTitle)
Record{
ForEach(record.itemArray){
obj in Textual content("(obj.title!)")
}.onDelete(carry out: deleteTestItem)
}
Button("Add merchandise", motion: {
addNewTestItem()
}).disabled(inputNotOk).buttonStyle(.borderedProminent)
TextField("merchandise title", textual content: $textual content)
}
}
personal func addNewTestItem(){
let newItem = TestItem(context: viewContext)
newItem.title = textual content
newItem.record = record
do{
attempt viewContext.save()
} catch {
let error = error as NSError
fatalError("Error (error), (error.userInfo)")
}
}
personal func deleteTestItem(offsets: IndexSet){
withAnimation{
offsets.map{testitems[$0]}.forEach(viewContext.delete)
}
do{
attempt viewContext.save()
} catch {
let error = error as NSError
fatalError("Error (error), (error.userInfo)")
}
}
}
If the record has objects, the app crashes when i enter ListElementsView. I can enter it if the record is empty.
I attempted doing print(sort(of: record.testitems))
, it returns NSSet
Then i attempted including print(record.testitems!.depend)
, the app crashes.
Any concepts?