//運行時獲取屬性,基本數據類型在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[];
}
}