Last active
March 29, 2020 20:27
-
-
Save JulesMoorhouse/2c811a8d79d75429f89691f25900a03b to your computer and use it in GitHub Desktop.
Revisions
-
JulesMoorhouse revised this gist
Mar 29, 2020 . 2 changed files with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ lazy var managedObjectModel: NSManagedObjectModel = { let modelURL = NSBundle.mainBundle().URLForResource("myDevices", withExtension: "Momd")! return NSManagedObjectModel(contentsOfURL: modelURL)! This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ class DeviceDetailTableViewController: UITableViewController { @IBOutlet weak var nameTextField: UITextField! @IBOutlet weak var deviceTypeTextField: UITextField! @IBOutlet weak var deviceOwnerLabel: UILabel! override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) -
JulesMoorhouse revised this gist
Mar 29, 2020 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -16,6 +16,12 @@ class DeviceDetailTableViewController: UITableViewController { if let device = device { nameTextField.text = device.name deviceTypeTextField.text = device.deviceType if let owner = device.owner { deviceOwnerLabel.text = "Device owner: \(owner.name)" } else { deviceOwnerLabel.text = "set device owner" } } } @@ -29,6 +35,7 @@ class DeviceDetailTableViewController: UITableViewController { device = Device(entity: entity, insertIntoManagedObjectContext: managedObjectContext) device?.name = name device?.deviceType = deviceType } } } @@ -41,6 +48,8 @@ class DeviceDetailTableViewController: UITableViewController { PeopleTableViewController { // more personPicker setup code here personPicker.pickerDelegate = self personPicker.selectedPerson = device?.owner navigationController?.pushViewController(personPicker, animated: true) } @@ -54,5 +63,11 @@ class DeviceDetailTableViewController: UITableViewController { extension DeviceDetailTableViewController: PersonPickerDelegate { func didSelectPerson(person: Person) { device?.owner = person do { try managedObjectContext.save() } catch { print("Error saving the managed object context!") } } } -
JulesMoorhouse revised this gist
Mar 29, 2020 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ import Foundation import CoreData extension Device { @nonobjc public class func fetchRequest() -> NSFetchRequest<Device> { return NSFetchRequest<Device>(entityName: "Device") } @NSManaged public var deviceType: String @NSManaged public var name: String @NSManaged public var owner: Person? } -
JulesMoorhouse revised this gist
Mar 29, 2020 . 1 changed file with 18 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -37,6 +37,22 @@ class DeviceDetailTableViewController: UITableViewController { NSIndexPath) { if indexPath.section == 1 && indexPath.row == 0 { if let personPicker = storyboard?.instatiateViewControllerWithIdentifier("People") as? PeopleTableViewController { // more personPicker setup code here navigationController?.pushViewController(personPicker, animated: true) } } } tableView.deselectRowAtIndexPath(indexPath, animated: true) } } extension DeviceDetailTableViewController: PersonPickerDelegate { func didSelectPerson(person: Person) { device?.owner = person } } -
JulesMoorhouse revised this gist
Mar 29, 2020 . 1 changed file with 13 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -24,9 +24,19 @@ class DeviceDetailTableViewController: UITableViewController { if device == nil { if let name = nameTextField.text, deviceType = deviceTypeTextField.text, entity = NSEntityDescription.entityForName("Device", inManagedObjectContext: managedObjectContext) where !name.isEmpty && !deviceType.isEmpty { device = Device(entity: entity, insertIntoManagedObjectContext: managedObjectContext) device?.name = name } } } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexpath: NSIndexPath) { if indexPath.section == 1 && indexPath.row == 0 { if let personPicker = storyboard?.instatiate ##### 9:14 } } -
JulesMoorhouse revised this gist
Mar 29, 2020 . 2 changed files with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,12 @@ class DeviceDetailTableViewController: UITableViewController { } override func viewWillDisappear(animated: Bool) { // need to add a device? if device == nil { if let name = nameTextField.text, deviceType = deviceTypeTextField.text, entity = NSEntityDescription.entityForName("Device", inManagedObjectContext: managedObjectContext) } } override func viewDidLoad() { This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ https://www.youtube.com/watch?v=uJuLk1niBYA --- add relationships Device - Owner, Destination - Person, leave optional Persion - Devices, Destination - Device, Inverse - Owner, not optional, Type - to many -
JulesMoorhouse revised this gist
Mar 29, 2020 . 1 changed file with 27 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ import CoreData import UIKit @objcMembers class DeviceDetailTableViewController: UITableViewController { var managedObjectContext: NSManagedObjectContext! var device Device? @IBOutlet weak var nameTextField: UITextField! @IBOutlet weak var deviceTypeTextField: UITextField! @IBOutlet weak var deviceOwnerTextField: UITextField! override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) if let device = device { nameTextField.text = device.name deviceTypeTextField.text = device.deviceType } } override func viewWillDisappear(animated: Bool) { } override func viewDidLoad() { super.viewDidLoad() -
JulesMoorhouse revised this gist
Mar 29, 2020 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,3 +6,12 @@ protocol PersonPickerDelegate: class { func didSelectPerson(person: Person) } @objcMembers class PeopleTableViewController: UITableViewController { var managedObjectContext: NSManagedObjectContext! var people = [Person]() // for person select mode weak var pickerDelegate: PersonPickerDelegate? var selectedPerson: Person? -
JulesMoorhouse revised this gist
Mar 29, 2020 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ import UIKit import CoreData protocol PersonPickerDelegate: class { func didSelectPerson(person: Person) } -
JulesMoorhouse revised this gist
Mar 29, 2020 . 1 changed file with 14 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,22 @@ let modelURL = NSBundle.mainBundle().URLForResource("myDevices", withExtension: "Momd")! return NSManagedObjectModel(contentsOfURL: modelURL)! }() lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { let coordinator = NSPersistentStoreCoordinator (managedObjectModel: self.managedObjectModel) let url = self.applicationDocumentDirectory. URLByAppendingPathComponent("SingleViewCoreData.sqlite") var failureReason = "There was an error creating or loading the application's saved data." do { try coordinator.addPersistenStoreWithType (NSSQLiteStoreType, configuration: nil, URL: url, options: [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]) } catch { var dict = [String: AnyObject]() dict[NSLocalisedDescriptionKey] = "Failed to initialize the application's saved data" -
JulesMoorhouse revised this gist
Mar 29, 2020 . 1 changed file with 14 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,19 @@ var failureReason = "There was an error creating or loading the application's saved data." do { try coordinator.addPersistenStoreWithType (NSSQLiteStoreType, configuration: nil, URL: url, options: nil) } catch { var dict = [String: AnyObject]() dict[NSLocalisedDescriptionKey] = "Failed to initialize the application's saved data" dict[NSUnderlyingErrorKey] = error as NSError let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)") abort() } return coordinator }() -
JulesMoorhouse revised this gist
Mar 29, 2020 . 1 changed file with 14 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)") abort() } return coordinator }() lazy var managedObjectContext: NSManagedObjectContext { } -
JulesMoorhouse revised this gist
Mar 28, 2020 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,11 @@ --- add relationships Device - Owner, Destination - Person, leave optional Persion - Devices, Destination - Device, Inverse - Owner, not optional, Type - to many Editor > Create NSManagedObjects Subclass, both In Devices.swift var deviceDescription: String { return "\(name) (\(deviceType))" } Remember again to remove the question mark from Person - name, devices. Device - all Strings -
JulesMoorhouse created this gist
Mar 28, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ --- add relationships Device - Owner, Destination - Person, leave optional Persion - Devices, Destination - Device, Inverse - Owner, not optional, Type - to many Editor > Create NSManagedObjects Subclass, both