Reputation: 1347
I have a custom NSManagedObject:
import Foundation
import CoreData
@objc(Article)
class Article: NSManagedObject {
@NSManaged var articleID: String
@NSManaged var isFavorite: Bool
init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?, articleID: String, isFavorite: Bool) {
super.init(entity: entity, insertIntoManagedObjectContext: context)
self.articleID = articleID
self.isFavorite = isFavorite
}
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) {
super.init(entity: entity, insertIntoManagedObjectContext: context)
}
}
But I got error when I trie to add a new entry to CoreData:
let articleEntity = NSEntityDescription.entityForName("Article", inManagedObjectContext: self.context!)
let newArticle = Article(entity: articleEntity!, insertIntoManagedObjectContext: self.context!)
newArticle.articleID = articleID
newArticle.isFavorite = true
Use of unresolved identifier 'Article'
Upvotes: 0
Views: 319
Reputation: 270
// 4) AppViewController
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
print("Document Directory:", FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last ?? "Not found!!")
return true
}
Upvotes: 0
Reputation: 270
// 1) Swift File
import Foundation import CoreData import UIKit
class DatabaseHelper{
static var shareInstance = DatabaseHelper()
let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext
func save(object:[String:String]){
let student = NSEntityDescription.insertNewObject(forEntityName: "Student", into: context!) as! Student
student.name = object["name"]
student.address = object["address"]
student.city = object["city"]
student.mobile = object["mobile"]
do{
try context?.save()
}catch{
print("Data is not save")
}
}
func getStudentData() -> [Student]{
var student = [Student]()
let fatchRequest = NSFetchRequest<NSManagedObject>(entityName: "Student")
do{
student = try context?.fetch(fatchRequest)as![Student]
}catch{
print("can not get Data")
}
return student
}
// 2) view Controller
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var txtCity: UITextField!
@IBOutlet weak var txtMobile: UITextField!
@IBOutlet weak var txtAddress: UITextField!
@IBOutlet weak var txtName: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func btnSaveData(_ sender: Any) {
var dict = ["name":txtName.text,"address":txtAddress.text,"city":txtCity.text,"mobile":txtMobile.text]
DatabaseHelper.shareInstance.save(object: dict as! [String : String])
}
@IBAction func btnShowAction(_ sender: Any) {
let show = storyboard?.instantiateViewController(withIdentifier: "AnotherViewController")as! AnotherViewController
self.navigationController?.pushViewController(show, animated: true)
}
}
// 3) Data get in table View
import UIKit
class AnotherViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
@IBOutlet weak var tblView: UITableView!
var student = [Student]()
override func viewDidLoad() {
super.viewDidLoad()
student = DatabaseHelper.shareInstance.getStudentData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return student.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tblView.dequeueReusableCell(withIdentifier: "tblCell", for: indexPath)as! tblCell
cell.lblName.text = student[indexPath.row].name
cell.lblAddress.text = student[indexPath.row].address
cell.lblCity.text = student[indexPath.row].city
cell.lblMobile.text = student[indexPath.row].mobile
return cell
}
} class tblCell : UITableViewCell{
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var lblAddress: UILabel!
@IBOutlet weak var lblCity: UILabel!
@IBOutlet weak var lblMobile: UILabel!
}
Upvotes: 0
Reputation: 5974
From the information above, it looks like you have't added the class for the entity in configuration of the coredata. Make sure you have mapped class against entity in the configuration of .xcdatamodeld
file. You can check the below example.
Upvotes: 1