这需求不陌生,直接上代码。
static var propertyNames: [String] {
var outCount: UInt32 = 0
guard let ivars = class_copyIvarList(self, &outCount) else {
return []
}
var result = [String]()
let count = Int(outCount)
for i in 0..<count {
let pro: Ivar = ivars[i]
guard let ivarName = ivar_getName(pro) else {
continue
}
guard let name = String(utf8String: ivarName) else {
continue
}
result.append(name)
}
return result
}
1,获取所有的属性
func isPropertyExisted(_ propertyName: String) -> Bool {
for name in UIAlertAction.propertyNames {
if name == propertyName {
return true
}
}
return false
}
2, 是否存在某个属性
func setTextColor(_ color: UIColor) {
let key = "_titleTextColor"
guard isPropertyExisted(key) else {
return
}
self.setValue(color, forKey: key)
}
3,修改颜色
作为UIAlertAction 的Extension 使用即可,这个不用我赘述了吧