首先我们创建一个class,添加如下成员变量
var list = Array<AnyObject>()
var name:String?
var i32 :UInt32?
var number :NSNumber?
var dict = Dictionary<String,AnyObject>()
我们分别通过Property和ivar来获取这些成员变量信息
-
property获取
var count:UInt32 = 0
var properList = class_copyPropertyList(self.classForCoder, &count)
for index in0 ... count-1 {
var item = properList[ Int(index)]
//获取变量名称
var proper_name = property_getName(item)
var properName = String.fromCString(proper_name)!
println("变量名称:" + properName )
//获取属性
var proper_attri = property_getAttributes(item)
var properAttri = String.fromCString(proper_attri)!
println( "变量属性:" + properAttri )
//获取属性value
var proper_attri_value = property_copyAttributeValue(item, proper_attri)
var properAttriValue:String? = String.fromCString(proper_attri_value)
println( "变量属性值:\(properAttriValue)" )
}
2.通过ivar获取
var count:UInt32 = 0
var ivar = class_copyIvarList(self.classForCoder, &count)
for index in0 ... count-1 {
var ivarName = ivar_getName( ivar[ Int(index) ] )
var ivarType = ivar_getTypeEncoding( ivar[ Int(index)])
var nEncodingType = String.fromCString(ivarType)!
var nName = String.fromCString(ivarName)!
println(" \(nName) \(nEncodingType) \(index)")
}
我们来看一下效果:
我们看到:通过property获取的少了UInt32的变量
然后我们把list设为private的再看一下效果:
我们看到property获取的结果中list木有了
微信扫一扫
关注该公众号