ios - swift mirror return class name in class function -
i'm having class function , in class function want name of class thats extending abstract entity class.
i found mirror type have this:
class abstractentity { class func findorcreateentity() -> anyobject? { print("name: \(nsstringfromclass(mirror(reflecting: self)))") } }
the problem example class car extends abstractentity , calls method prints car.type, correct don't want .type extension. there way class name?
i want swift way if it's possible. know of exnteions of nsobject , nsstringfromclass thing..
for clarity:
class abstractentity: nsmanagedobject { // insert code here add functionality 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 { 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 nserror { ddlogerror("fetch failed: \(error.localizeddescription)") } return nil } }
that's abstractentity , every entity in model extends that. have 1 place want logic.
now example have car entity , want car.find... , returns me car. why (think) need class name can use fetch nsfetchrequest(entityname: entityname). pass in entityname want fetch think ugly because when car.find.. know want car.
really don't question, according requirement need following functionality.
class abstractentity { class func findorcreateentity() { let str = __file__ let fullnamearr = str.componentsseparatedbystring("/") print("name: \(fullnamearr.last!)") } }
Comments
Post a Comment