1、分類會覆寫本類的方法?
1. category的方法沒有“完全替換掉”原來類已經有的方法,也就是說如果category和原來類都有methodA,那麼category附加完成之後,類的方法清單裡會有兩個methodA 2. category的方法被放到了新方法清單的前面,而原來類的方法被放到了新方法清單的後面,這也就是我們平常所說的category的方法會“覆寫”掉原來類的同名方法,這是因為運作時在查找方法的時候是順着方法清單的順序查找的,它隻要一找到對應名字的方法,就會罷休^_^,殊不知後面可能還有一樣名字的方法
2、如何調用被分類覆寫的方法? + (void)useClassMethodInsteadCayegoryMethod: (SEL)seletor { if (self) { unsigned int methodCount; Method *methodList = class_copyMethodList([self class], &methodCount); IMP lastImp = NULL; SEL lastSel = NULL; for (NSInteger i = 0; i < methodCount; i++) { Method method = methodList[i]; NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(method)) encoding:NSUTF8StringEncoding]; NSString *selectorName = NSStringFromSelector(seletor); if ([selectorName isEqualToString:methodName]) { lastImp = method_getImplementation(method); lastSel = method_getName(method); } } typedef void (*fn)(id,SEL); if (lastImp != NULL) { fn f = (fn)lastImp; f(self,lastSel); } free(methodList); }}