gavsta707 gavsta707 - 2 years ago 108
Swift Question

Connecting my NSManagedObject subclass with my ViewController.swift


This is probably an extremely dumb/simple question, but it seems to be throwing me through a loop. I have created a NSManagedObject subclass for my entity "UsedInfo" per the advice of my fellows on StackOverflow. My ultimate goal is to use this subclass to send User information to CoreData, and later retrieve it.


The issue is that i cannot figure out how to use my new file, "UsedInfo+CoreDataProperties.swift", with my "ViewController.swift" file, which is where my TextFields are connected. Below you'll find the relevant code.

Code for ViewController.swift (SaveButton)

@IBAction func saveButton(sender: AnyObject) {
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext
let managedContext:NSManagedObjectContext = appDel.managedObjectContext
let entity1 = NSEntityDescription.insertNewObjectForEntityForName("UsedInfo", inManagedObjectContext:context) as NSManagedObject

let one = pickerTextField.text
let two = modelName.text
let three = serialNo.text
let four = YOM.text
let five = engineHours.text
let six = locationOfMachine.text

entity1.setValue(one, forKey: "product")
entity1.setValue(two, forKey:"modelName")
entity1.setValue(three, forKey:"serialNo")
entity1.setValue(four, forKey:"yom")
entity1.setValue(five, forKey:"engineHours")
entity1.setValue(six, forKey:"location")

do {
catch {


Code for "UsedInfo+CoreDataProperties.swift"

import Foundation
import CoreData

extension UsedInfo {

@NSManaged var engineHours: String?
@NSManaged var location: String?
@NSManaged var modelName: String?
@NSManaged var product: String?
@NSManaged var serialNo: String?
@NSManaged var yom: String?


Code for "UsedInfo.swift"

import Foundation
import CoreData
class UsedInfo: NSManagedObject {

//Insert code here to add functionality to your managed object subclass


I thank you folks in advance. I'm sorry for my utter noob-ness.

Answer Source

Since you created a subclass of NSManagedObject, you can use that subclass without any special steps to "connect" it. It's ready, and it can do anything defined by NSManagedObject as well as anything added in the new subclass.

With Core Data you'd start by changing the code that creates a new instance to be something like

let entity1 = NSEntityDescription.insertNewObjectForEntityForName("UsedInfo", inManagedObjectContext:context) as NSManagedObject as! UsedInfo

The call to insertNewObjectForEntityForName(_:inManagedObjectContext:) will create an instance of UsedInfo, but you need to add the as! UsedInfo to make it clear that's what you're getting. Using as! can be dangerous but it's not a bad idea here, because if this downcast fails, you want to know immediately so that you can fix your managed object model.

After that, entity1 is an instance of your new UsedInfo class. You can use the properties declared on UsedInfo in the code below. For example,

entity1.product = one
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download