diff --git a/CoreDataDemo.xcodeproj/project.pbxproj b/CoreDataDemo.xcodeproj/project.pbxproj index 467e92f..1286b7c 100644 --- a/CoreDataDemo.xcodeproj/project.pbxproj +++ b/CoreDataDemo.xcodeproj/project.pbxproj @@ -99,6 +99,7 @@ TargetAttributes = { 9B71A4121F9BE73000E8ADE6 = { CreatedOnToolsVersion = 9.0; + LastSwiftMigration = 1140; ProvisioningStyle = Automatic; }; }; @@ -283,7 +284,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = shashi.CoreDataDemo; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.0; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -297,7 +298,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = shashi.CoreDataDemo; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.0; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; diff --git a/CoreDataDemo/AppDelegate.swift b/CoreDataDemo/AppDelegate.swift index f4c04c3..08ded3d 100644 --- a/CoreDataDemo/AppDelegate.swift +++ b/CoreDataDemo/AppDelegate.swift @@ -15,7 +15,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } diff --git a/CoreDataDemo/Base.lproj/Main.storyboard b/CoreDataDemo/Base.lproj/Main.storyboard index 03c13c2..c7818b6 100644 --- a/CoreDataDemo/Base.lproj/Main.storyboard +++ b/CoreDataDemo/Base.lproj/Main.storyboard @@ -1,7 +1,9 @@ - + + - + + @@ -9,16 +11,91 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/CoreDataDemo/CoreDataDemo.xcdatamodeld/CoreDataDemo.xcdatamodel/contents b/CoreDataDemo/CoreDataDemo.xcdatamodeld/CoreDataDemo.xcdatamodel/contents index 492c086..989d128 100644 --- a/CoreDataDemo/CoreDataDemo.xcdatamodeld/CoreDataDemo.xcdatamodel/contents +++ b/CoreDataDemo/CoreDataDemo.xcdatamodeld/CoreDataDemo.xcdatamodel/contents @@ -1,11 +1,11 @@ - + - - - + + + - + \ No newline at end of file diff --git a/CoreDataDemo/ViewController.swift b/CoreDataDemo/ViewController.swift index b6ab2db..6fdf8d6 100644 --- a/CoreDataDemo/ViewController.swift +++ b/CoreDataDemo/ViewController.swift @@ -10,31 +10,94 @@ import UIKit import CoreData class ViewController: UIViewController { + // MARK: Outlets + @IBOutlet weak var clearButton: UIButton! + @IBOutlet weak var submitButton: UIButton! + @IBOutlet weak var fetchButton: UIButton! + @IBOutlet weak var ageLabel: UILabel! + @IBOutlet weak var passwordLabel: UILabel! + @IBOutlet weak var nameLabel: UILabel! + @IBOutlet weak var dobPicker: UIDatePicker! + @IBOutlet weak var enterPassword: UITextField! + @IBOutlet weak var enterName: UITextField! + // MARK: overrides override func viewDidLoad() { super.viewDidLoad() - + fetchButton.isEnabled = false + submitButton.isEnabled = false + + [enterPassword, enterName, dobPicker].forEach({ $0.addTarget(self, action: #selector(editingChanged), for: .editingChanged) }) + + // Do any additional setup after loading the view, typically from a nib. + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + // MARK: Field Actions + + @objc func editingChanged(_ textField: UITextField) { + if textField.text?.count == 1 { + if textField.text?.first == " " { + textField.text = "" + return + } + } + guard + let name = enterName.text, !name.isEmpty, + let password = enterPassword.text, !password.isEmpty + else { + submitButton.isEnabled = false + return + } + submitButton.isEnabled = true + + } + + @IBAction func submitAction(_ sender: UIButton) { let appDelegate = UIApplication.shared.delegate as! AppDelegate - let context = appDelegate.persistentContainer.viewContext - let entity = NSEntityDescription.entity(forEntityName: "Users", in: context) let newUser = NSManagedObject(entity: entity!, insertInto: context) - - - newUser.setValue("Shashikant", forKey: "username") - newUser.setValue("1234", forKey: "password") - newUser.setValue("12", forKey: "age") - + + newUser.setValue(enterName.text, forKey: "username") + newUser.setValue(enterPassword.text, forKey: "password") + let date = Date() + let calendar = Calendar.current + _ = calendar.component(.hour, from: date) + _ = calendar.component(.minute, from: date) + + let age = calendar.dateComponents([.year], from: date, to: dobPicker.date) + print("age in years =\(String(describing: abs(age.year!)))") + newUser.setValue(String(abs(age.year!)), forKey: "age") do { - try context.save() - + fetchButton.isEnabled = true } catch { - print("Failed saving") } - + } + + @IBAction func clearDataBase(_ sender: UIButton) { + let appDelegate = UIApplication.shared.delegate as! AppDelegate + let fetchRequest: NSFetchRequest = NSFetchRequest(entityName: "Users") + let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) + let context = appDelegate.persistentContainer.viewContext + + do { + try context.execute(deleteRequest) + clearButton.isEnabled = false + } catch let error as NSError { + // TODO: handle the error + print(error) + } + } + @IBAction func fetchAction(_ sender: UIButton) { + let appDelegate = UIApplication.shared.delegate as! AppDelegate + let context = appDelegate.persistentContainer.viewContext let request = NSFetchRequest(entityName: "Users") //request.predicate = NSPredicate(format: "age = %@", "12") request.returnsObjectsAsFaults = false @@ -42,22 +105,13 @@ class ViewController: UIViewController { do { let result = try context.fetch(request) for data in result as! [NSManagedObject] { - print(data.value(forKey: "username") as! String) + nameLabel.text = (data.value(forKey: "username") as! String) + passwordLabel.text = (data.value(forKey: "password") as! String) + ageLabel.text = (data.value(forKey: "age") as! String) } - } catch { - print("Failed") } - - // Do any additional setup after loading the view, typically from a nib. } - - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } - - }