美文网首页
Swift运行时获取属性

Swift运行时获取属性

作者: 飘金 | 来源:发表于2017-04-19 18:04 被阅读0次

//运行时获取属性,基本数据类型在OC中没有可选,如果定义成可选,运行时获取不到,使用KVC就会奔溃

//private的属性,运行时同样获取不到,使用KVC就会奔溃

类必须继承NSObject,否则获取不到属性

classPerson:NSObject{

varname ="piaojin"

varage =20

varvalue ="love"

classfuncgetPropertyList() -> [String]{

varcount :UInt32=0

//获取类的属性列表,返回属性列表数组(可选值)

letlist =class_copyPropertyList(self, &count)

print("属性的数量:\(count)")

//遍历数组

foriin0..

//根据下标获取属性

guardletpty = list?[i],

//获取属性的名称,返回的是c语言的char

letcName =property_getName(pty),

//转成String

letstr =String(utf8String:cName)else{

continue

}

print(str)

}

//释放C语言的对象

free(list)

return[];

}

}

相关文章

网友评论

      本文标题:Swift运行时获取属性

      本文链接:https://www.haomeiwen.com/subject/rnyhzttx.html