0

I'm having a class function and in that class function I want the name of the class thats extending this abstract entity class.

I found the Mirror type could do this so I have something like this:

class AbstractEntity {

    class func findOrCreateEntity() -> AnyObject? {
        print("Name: \(NSStringFromClass(Mirror(reflecting: self)))")
    }
}

The only problem is that for example a class Car extends this AbstractEntity and it calls the method this prints Car.Type, this is correct but I don't want the .Type extension. Is there any way to just get the class name?

I really want to do it the swift way if it's possible. I know of the exnteions of NSObject and then the NSStringFromClass thing..

For clarity:

class AbstractEntity: NSManagedObject {

    // Insert code here to add functionality to your managed object subclass
    class func findOrCreateEntityWithUUID(entityName: String, uuid: String, context: NSManagedObjectContext) -> AnyObject? {
        let request = NSFetchRequest(entityName: entityName)
        request.returnsObjectsAsFaults = false;

        let predicate = NSPredicate(format: "uuid = %@", uuid)
        request.predicate = predicate

        do {
            var results = try context.executeFetchRequest(request)
            if results.count > 0 {
                return results[0]
            } else {
                let entity =  NSEntityDescription.entityForName(entityName, inManagedObjectContext: context)

                let abstractEntity = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: context)
                abstractEntity.setValue(uuid, forKey: "uuid")

                return abstractEntity
            }
        } catch let error as NSError {
            DDLogError("Fetch failed: \(error.localizedDescription)")
        }

        return nil
    }
}

That's my AbstractEntity and every entity in my model extends that. So I have one place where I want my logic.

Now for example I have a Car entity and I want to do Car.find... and it returns me a Car. That why I (think) I need the class name so that I can use it to fetch it with the NSFetchRequest(entityName: entityName). I now pass in the entityName I want to fetch but I think this is ugly because when you do Car.find.. you know you want a car.

user1007522
  • 7,858
  • 17
  • 69
  • 113
  • If you make the function generic, could you not use T.self? – jjatie Jul 12 '16 at 13:04
  • Why can't each class just return its own name in the format you want? – Droppy Jul 12 '16 at 13:07
  • It's a function on Core Data types. So I have a AbstractEntity and that's where I want to bundle the logic to find and create all the others that extend the class. So I don't think I can use T? Droppy how do you mean? I want to do Car.findOrCreateEntity and in the AbstractEntity I need the name Car because I need to fetch the Car entity in core data. – user1007522 Jul 12 '16 at 13:13
  • 1
    I assume you already know about http://stackoverflow.com/questions/27109268/how-can-i-create-instances-of-managed-object-subclasses-in-a-nsmanagedobject-swi/34004264#34004264. That is "the Swift way." That you would use Core Data (which is Apple-only) but avoid Foundation (which has a cross-platform, opensource version written in Swift) is drawing a very odd line. Note that this will be built-into iOS 10 (and I'm sure they'll be calling Foundation under the covers). That said, if you're able to generate `Car.Type`, just split it at the `.` and take the first part. – Rob Napier Jul 12 '16 at 15:04

1 Answers1

0

Really i don't get your question, but according to my requirement you need following functionality.

class AbstractEntity {


    class func findOrCreateEntity() {
        let str = __FILE__
        let fullNameArr = str.componentsSeparatedByString("/")
        print("Name: \(fullNameArr.last!)")


    }
}
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
  • Thanks man. I've updated my question with example code of the AbstractEntity class with explanation of why I want to do that. I now do Car.find..("car", .. ) I just want to eliminate that "car" var. – user1007522 Jul 12 '16 at 13:26