0
votes

I am new to iOS development , can you help me anyone in bug point of you. thank you for advance.

uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Project1.'

 let  context  = appdelegate.persistentContainer.viewContext
 let proj = Project() 
        let arrProj = dic.object(forKey: "Projects") as! NSArray
        for n in 0..<arrProj.count {
            let subDic = arrProj.object(at: n) as! NSDictionary
            let item = ProjectItem(dict: subDic)
            proj.arrProjs.append( item )


           let projects = NSEntityDescription.entity(forEntityName: "Projects", in: context)
            projects?.setValue(item.Project, forKey: "project1")
            projects?.setValue(item.Project2, forKey: "project2")
            projects?.setValue(item.ID, forKey: "projectid")
            projects?.setValue(item.radius, forKey: "radius")
            projects?.setValue(item.GeofenceType, forKey: "geo_Type")
            projects?.setValue(item.Geofence, forKey: "geofence")
            projects?.setValue(item.Coordinates, forKey: "coordinates")
        }
2
when this error is comes ?Yogesh Patel
Project have NSObject class ?Yogesh Patel
thank you for response , I am using CoreData database , while saving time , facing this issue can you help meuser11211835
@YogeshPatel Project is another class , remaining working fine but let projects = NSEntityDescription.entity(forEntityName: "Projects", in: context) projects?.setValue(item.Project, forKey: "project1") only problemuser11211835
Please check your outlets in xib or storyboard make sure all perfectly attached. please let me know ?Yogesh Patel

2 Answers

2
votes

You should set values in NSManagedObject object, not in NSEntityDescription object

let entity = NSEntityDescription.entity(forEntityName: "Projects", in: context)
for n in 0..<arrProj.count {
    let projects = NSManagedObject(entity: entity!, insertInto: context)
    projects?.setValue(item.Project, forKey: "project1")
    projects?.setValue(item.Project2, forKey: "project2")
    //...
}
2
votes

Please check this using insertobject query you can save your data in core data.

    let  context  = appdelegate.persistentContainer.viewContext
    let projects = NSEntityDescription.insertNewObject(forEntityName: "Projects", into: context) as! Projects

    projects.project1 = item.Project
    projects.project2 = item.Project2
    or
    projects?.setValue(item.Project, forKey: "project1")
    projects?.setValue(item.Project2, forKey: "project2")
    projects?.setValue(item.ID, forKey: "projectid")
    projects?.setValue(item.radius, forKey: "radius")

    do{
    try context.save()
    }catch{
    print("error")
    }