i use core data in my app. I want to parse core data's data to struct but i can't do this. I can get datas from core data. However don't know how can i parse those datas to my struct.
I know codeble json parse, if can we use that way parse to core data it will be great.
struct strUser: Codable {
let city, country, createdDate, url, userType, validDate, fullName, bio, languages, mail, name, organization, password, photo, profession, role, surname, title: String
let isActive: Bool
enum CodingKeys: String, CodingKey {
case city = "city"
case country = "country"
case createdDate = "created_date"
case url = "url"
case userType = "user_type"
case validDate = "valid_date"
case fullName = "full_name"
case bio = "bio"
case languages = "languages"
case mail = "mail"
case name = "name"
case organization = "organization"
case password = "password"
case photo = "photo"
case profession = "profession"
case role = "role"
case surname = "surname"
case title = "title"
case isActive
}
}
@objc func getUser(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
fetchRequest.returnsObjectsAsFaults = false
do {
let results = try context.fetch(fetchRequest)
print(results)
} catch {
print("error")
}
}
I use that func for call my user from core data and i give my users like this ;
data: {
bio = "I am a echonomist";
city = Newyork;
country = USA;
"created_date" = "01-07-2019";
"full_name" = "Barney Stinson";
"is_active" = 1;
languages = English;
mail = "[email protected]";
name = Barney;
organization = GNB;
password = 1234;
photo = image;
profession = Echonomist;
role = Cordinator;
surname = Stinson;
title = title;
url = "www.stinson.com";
"user_type" = normal;
"valid_date" = "01-07-2019";
}), <User: 0x600003957070> (entity: User; id: 0x9f8fcaa8f7a43527 <x-coredata://2AF3AC56-9A78-4F48-89AB-655B4129234E/User/p2> ; data: {
bio = "I am a architect";
city = Paris;
country = France;
"created_date" = "02-07-2019";
"full_name" = "Ted Evelyn Mosby";
"is_active" = 1;
languages = English;
mail = "[email protected]";
name = "Ted Evelyn";
organization = "Mosby Architectonics";
password = 2345;
photo = image2;
profession = Architect;
role = Architect;
surname = Mosby;
title = title;
url = "www.mosby.com";
"user_type" = normal;
"valid_date" = "02-07-2019";
}), <User: 0x6000039570c0> (entity: User; id: 0x9f8fcaa8f7a03527 <x-coredata://2AF3AC56-9A78-4F48-89AB-655B4129234E/User/p3> ; data: {
bio = "I am a lawyer";
city = Roma;
country = Italy;
"created_date" = "03-07-2019";
"full_name" = "Marshall Eriksen";
"is_active" = 1;
languages = English;
mail = "[email protected]";
name = Marshall;
organization = "World Health Organization";
password = 3456;
photo = image3;
profession = Lawyer;
role = Lawyer;
surname = Eriksen;
title = title;
url = "www.erisen.com";
"user_type" = normal;
"valid_date" = "03-27-2019";
})))]
Those are print(results) reason.
I would suggest writing an initializer for your User Struct (the JSON version) that takes the NSManagedObject (Core Data) User as its argument. ie:
extension StructUser {
init(record: User) {
// initialize all properties, ie:
self.city = record.city
// etc.
}
}
Then, you can map the objects into the Codable User struct types
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
let managedObjectUsers = try! context.fetch(fetchRequest) as! [User]
let codableUsers = managedObjectUsers.map { StructUser.init(record: $0) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With