Last active
May 8, 2019 08:10
-
-
Save post799/dde8c65ec6cf3ad5343cd41b62d1775f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Entity = SaveData , Attribute = mydata , First Text Filed Name : textField1 ,Second Text Field Name: textField2 | |
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
//1.Save the Data | |
//Refer to Appdelegate | |
let appDelegate = UIApplication.shared.delegate as! AppDelegate | |
//Create Context | |
let context = appDelegate.persistentContainer.viewContext | |
//Create Entity AND NEW Entity | |
let entity = NSEntityDescription.entity(forEntityName: "SaveData", in: context) | |
let newSaveData = NSManagedObject(entity: entity!, insertInto: context) | |
//Create NEW Record | |
newSaveData.setValue(textField1.text, forKey: "mydata") | |
//Save it | |
do { | |
try context.save() | |
} catch { | |
print("Failed saving data") | |
} | |
//2. Retrieve Data | |
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "SaveData") | |
request.returnsObjectsAsFaults = false | |
do { | |
let result = try context.fetch(request) | |
for data in result as! [NSManagedObject] { | |
textField2.text = (data.value(forKey: "mydata") as! String) | |
} | |
} catch { | |
print("Failed") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment