前面對runtime有了一個簡單瞭解,可看了半天在應用在沒什麼用啊,那你就錯了編碼
啊,我想起來了,你前面不就要說應用嗎,爲毛等到如今,我還傻傻看完前一頁,流量啊,其實我是來替網通作任務的spa
還記得歸檔解檔嗎,想一想就心碎的不行啊,那一大堆的=啊,有了runtime立刻心就熱乎了指針
首先遵循NSCoding協議code
//歸檔 - (void)encodeWithCoder:(NSCoder *)aCoder { unsigned int count; //得到指向當前類的全部屬性的指針 objc_property_t *propertys = class_copyPropertyList([Student class], &count); for (int i = 0; i < count; i++) { //得到指向屬性的指針 objc_property_t property = propertys[i]; const char *name = property_getName(property); NSString *properyName = [NSString stringWithUTF8String:name]; //經過名稱取值 NSString *propertyValue = [self valueForKey:properyName]; //編碼屬性 [aCoder encodeObject:propertyValue forKey:properyName]; } free(propertys); } //解檔 - (instancetype)initWithCoder:(NSCoder *)aDecoder { unsigned int count; //得到指向當前類的全部屬性的指針 objc_property_t *propertys = class_copyPropertyList([Student class], &count); for (int i = 0; i < count; i++) { //得到指向屬性的指針 objc_property_t property = propertys[i]; const char *name = property_getName(property); NSString *properyName = [NSString stringWithUTF8String:name]; //解碼屬性值 NSString *propertyValue = [aDecoder decodeObjectForKey:properyName]; [self setValue:propertyValue forKey:properyName]; } free(propertys); return self; }