0

I've been working with CoreData for one of my iOS apps and wanted to build a helper function to turn a String representation of a CoreData entity into an NSManagedObject instance of that entity. I've just finished my first Swift course for my major and from my understanding, I assume the logic/implementation is fairly simple. I believe my implementation is wrong because Swift is my newest programming language. I'm trying to avoid hard-coding a solution because my intuition tells me there's a better, more general way to achieve my goal.

Example (Naïve Pseudocode):

func getEntity(from string: String) -> NSManagedObject {
    return string as! NSManagedObject
}

Desired Execution:

let stringRep = "Entity"
entity(from: stringRep)

Entity

Context:

Two of the CoreData entities I'm working with are Category and Service.

public class Category: NSManagedObject { }
public class Service: NSManagedObject { }

Category contains a relationship to many Service entities and my other helper functions in AppService access CoreData using that relationship.

CategoryTableViewController has three static cells, each containing a String representation of the entities for Category.

I'm passing the selected String representation of an entity such as Service during prepare(for segue: UIStoryboardSegue, sender: Any?) to the next UIViewController with a UITableView.

Using the new helper function, I will be able to ambiguously fetch from CoreData with the String representation and display the data in the UITableView.

My ultimate goal is to cut out unnecessary functions in AppService with the function below.

func get(_ entities: String, for category: Category) -> NSFetchedResultsController<T> {
    let someEntity = getEntity(from: entities)
    let fetchRequest: NSFetchRequest<someEntity> = someEntity.fetchRequest()
    fetchRequest.predicate = NSPredicate(format: "category == %@", category)
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]

    return createFetchedResultsController(for: fetchRequest)
}

Example:

get("Service", for: category)
JD Smith
  • 47
  • 2
  • 10
  • You can't do that because `String` and `NSManagedObject` are not related. And `NSManagedObject` is not an entity. – vadian Apr 15 '17 at 17:50
  • I clarified it. That's just how I write pseudocode for Swift because of the language's semantics. – JD Smith Apr 15 '17 at 21:33

2 Answers2

2

You can create a new object of a certain entity based on the entity name. As always, you will need your managed object context.

First you have to get the NSEntityDescription:

let entity = NSEntityDescription.entity(forEntityName: "Widget", in: context)

Then you can create the object:

let newWidget = NSManagedObject(entity: entity, in: context)

However, it would be much better to use the subclass. It includes the entity information automatically.

let newWidget = Widget(context: context)  // much easier
JD Smith
  • 47
  • 2
  • 10
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thanks, this helps a lot! Documentation was a bit vague on `NSEntityDescription` and how to use it. It should fit perfectly into my `AppService`. – JD Smith Apr 15 '17 at 21:45
0

It's not possible in that way...

1,Suppose there is an Entity names as "Person", in that you have an attribute as "name"

2, have an NSManagedObject something like xx12145ac through fetch query OR Create a new NSManagedobject of person

3,once you have that object of type Person then you can set

myPersonObj.name = "SomeOne" as! String

I hope that will help you out!!!

Do let me know if you face any issue .. :)

Chetan A
  • 73
  • 8