Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing static variables of a model class using reflection

I have a model class that contains some static variables and properties. In Runtime I can get the properties;

let instance = entity.init()

let mirror = Mirror(reflecting:instance)

var propertyStrings = [String]()

for (propertyName, childMirror) in mirror.children {

}

But I want to get static variables of the class as a list too. So how can I get the list of static variable's names and values ? Here is my model class' structure :

class ActionModel: NSObject {

static let kLastModified = "LastModified"
static let kEntityName = "EntityName"
static let kIdentifier = "Id"


var lastModified: Int64
var entityName: String?
var identifier : PrimaryKeyString
like image 381
Mustafa Sait Demirci Avatar asked Feb 09 '16 14:02

Mustafa Sait Demirci


1 Answers

Getting type properties via reflection is not supported yet. You can see this by inspecting the displayStyle of the Mirror you get if you pass the class object to the initializer:

let mirror = Mirror(reflecting: ActionModel.self)
print(mirror.displayStyle)    // nil
like image 84
Alberto Doda Avatar answered Oct 01 '22 17:10

Alberto Doda