先來看一段代碼
class? LTTech{
? ? var?age: Int=10
? ? func?tech(){
? ? ? ? print("***tech***")
? ? }
? ??@objc var gender: Int = 1
? ??@objc func findTech(){
? ??????print("***findTech***")
? ? }
}
let?t = LTTech()
func?test(){
? ? var?methodCount: UInt32=0
? ? let?methodList = class_copyMethodList(LTTech.self, &methodCount)
? ? for i?in?0?..< numericCast(methodCount) {
? ? ? ? if?let?method = methodList?[i]{
? ? ? ? ? ? let?methodName = method_getName(method)
? ? ? ? ? ? print("方法列表:\(String(describing: methodName))")
? ? ? ? }else{
? ? ? ? ? ? print("not found method")
? ? ? ? }
? ? }
? ? var?count: UInt32=0
? ? let?propertyList = class_copyPropertyList(LTTech.self, &count)
? ? for i?in 0?..< numericCast(count) {
? ? ? ? if?let?property = propertyList?[i] {
? ? ? ? ? ? let?propertyName = property_getName(property)
? ? ? ? ? ? print("屬性成員屬性:\(String(describing: propertyName))")
? ? ? ? }else{
? ? ? ? ? ? print("not found property")
? ? ? ? }
? ? }
}
test()
//將上面代碼中LTTech潭兽,替換為如下代碼
class?LTTech: NSObject {
? ? var?age: Int=10
? ? func?tech(){
? ? ? ? print("***tech***")
? ? }
}
通過run代碼势腮,發(fā)現(xiàn)沒有輸出任何內容韩容,所以對于原生的Swift來說赤屋,是沒有runtime特性的
結論:
1志于、對于純Swift類來說,方法和屬性不加任何修飾符的情況下娘摔。這個時候其實已經不具備所謂的Runtime特性
2惹盼、對于純Swift類,方法和屬性添加@objc標識的情況下惑灵,當前可以通過Runtime API拿到山上,但是在OC中是沒法進行調度的
3、對于繼承自NSObject類來說英支,如果想要動態(tài)的獲取當前的屬性和方法佩憾,必須在其聲明前添加@objc關鍵字,否則也是沒有辦法通過Runtime API獲取的
4干花、純Swift類沒有動態(tài)性妄帘,但在方法、屬性前添加dynamic修飾池凄,可獲得動態(tài)性
5抡驼、繼承自NSObject的swift類,其繼承自父類的方法具有動態(tài)性肿仑,其他自定義方法致盟、屬性想要獲得動態(tài)性,需要添加dynamic修飾
6尤慰、若方法的參數(shù)馏锡、屬性類型為Swift特有、無法映射到OC的類型(如Character伟端、Tuple)杯道,則此方法、屬性無法添加dynamic修飾