Skip to content

Instantly share code, notes, and snippets.

@JulesMoorhouse
Last active March 29, 2020 20:27
Show Gist options
  • Select an option

  • Save JulesMoorhouse/2c811a8d79d75429f89691f25900a03b to your computer and use it in GitHub Desktop.

Select an option

Save JulesMoorhouse/2c811a8d79d75429f89691f25900a03b to your computer and use it in GitHub Desktop.

Revisions

  1. JulesMoorhouse revised this gist Mar 29, 2020. 2 changed files with 2 additions and 1 deletion.
    1 change: 1 addition & 0 deletions AppDelegate.swift
    Original 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)!
    2 changes: 1 addition & 1 deletion DeviceDetailTableViewController.swift
    Original 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 deviceOwnerTextField: UITextField!
    @IBOutlet weak var deviceOwnerLabel: UILabel!

    override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
  2. JulesMoorhouse revised this gist Mar 29, 2020. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions DeviceDetailTableViewController.swift
    Original 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!")
    }
    }
    }
  3. JulesMoorhouse revised this gist Mar 29, 2020. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions Device+CoreDataProperties.swift
    Original 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?

    }
  4. JulesMoorhouse revised this gist Mar 29, 2020. 1 changed file with 18 additions and 2 deletions.
    20 changes: 18 additions & 2 deletions DeviceDetailTableViewController.swift
    Original 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?.instatiate ##### 9:14
    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
    }
    }
  5. JulesMoorhouse revised this gist Mar 29, 2020. 1 changed file with 13 additions and 3 deletions.
    16 changes: 13 additions & 3 deletions DeviceDetailTableViewController.swift
    Original 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)
    inManagedObjectContext: managedObjectContext) where !name.isEmpty
    && !deviceType.isEmpty {
    device = Device(entity: entity, insertIntoManagedObjectContext:
    managedObjectContext)
    device?.name = name
    }
    }
    }

    override func viewDidLoad() {
    super.viewDidLoad()
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexpath:
    NSIndexPath) {
    if indexPath.section == 1 && indexPath.row == 0 {
    if let personPicker =
    storyboard?.instatiate ##### 9:14
    }
    }
  6. JulesMoorhouse revised this gist Mar 29, 2020. 2 changed files with 8 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion DeviceDetailTableViewController.swift
    Original 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() {
    2 changes: 2 additions & 0 deletions code.txt
    Original 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
  7. JulesMoorhouse revised this gist Mar 29, 2020. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions DeviceDetailTableViewController.swift
    Original 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()
  8. JulesMoorhouse revised this gist Mar 29, 2020. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions PeopleTableViewController.swift
    Original 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?

  9. JulesMoorhouse revised this gist Mar 29, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions PeopleTableViewController.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@

    import UIKit
    import CoreData

    protocol PersonPickerDelegate: class {
    func didSelectPerson(person: Person)
    }

  10. JulesMoorhouse revised this gist Mar 29, 2020. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion AppDelegate.swift
    Original 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: nil)
    configuration: nil, URL: url, options:
    [NSMigratePersistentStoresAutomaticallyOption: true,
    NSInferMappingModelAutomaticallyOption: true])
    } catch {
    var dict = [String: AnyObject]()
    dict[NSLocalisedDescriptionKey] = "Failed to initialize the application's saved data"
  11. JulesMoorhouse revised this gist Mar 29, 2020. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions AppDelegate.swift
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,19 @@

    let wrappedError = NSError(domain:
    "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
    var failureReason = "There was an error creating or loading the application's saved data."

    NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
    abort()
    }
    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
    }()

  12. JulesMoorhouse revised this gist Mar 29, 2020. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions AppDelegate.swift
    Original 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 {


    }
  13. JulesMoorhouse revised this gist Mar 28, 2020. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion code.txt
    Original 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
    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
  14. JulesMoorhouse created this gist Mar 28, 2020.
    4 changes: 4 additions & 0 deletions code.txt
    Original 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