#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif
// Class ISA;
Class superclass;
cache_t cache; // formerly cache pointer and vtable
class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags
......
}
由此可見,結構體objc_class也是繼承objc_object,說明Class在設計中本身也是一個對象。
int ivar_count OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
/* variable length structure */
struct objc_ivar ivar_list[1] OBJC2_UNAVAILABLE;
}
objc_ivar_list其實就是一個鏈表,存儲多個objc_ivar,而objc_ivar結構體存儲類的單個成員變量信息。
struct objc_method_list *obsolete OBJC2_UNAVAILABLE;
int method_count OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
/* variable length structure */
struct objc_method method_list[1] OBJC2_UNAVAILABLE;
}
同理,objc_method_list也是一個鏈表,存儲多個objc_method,而objc_method結構體存儲類的某個方法的信息。
iOS分類(category),類擴展(extension)—史上最全攻略
背景:
在大型項目,企業級開發中多人同時維護同一個類,此時程序員A因為某項需求只想給當前類currentClass添加一個方法newMethod,那該怎么辦呢?
最簡單粗暴的方式是把newMethod添加到currentClass中,然后直接實現該方法就OK了。
但考慮到OC是單繼承的,子類可以擁有父類的方法和屬性。
如果把newMethod寫到currentClass中,那么currentClass的子類也會擁有newMethod。但真正的需求是只需要currentClass擁有newMethod,而currentClass的子類不會擁有。
蘋果為了解決這個問題,就引入了分類(Category)的概念。
分類(Category):
概念
分類(Category)是OC中的特有語法,它是表示一個指向分類的結構體的指針。原則上它只能增加方法,不能增加成員(實例)變量。具體原因看源碼組成:
Category源碼:
Category Category 是表示一個指向分類的結構體的指針,其定義如下: typedef struct objc_category *Category; struct objc_category { char *category_name OBJC2_UNAVAILABLE; // 分類名 char *class_name OBJC2_UNAVAILABLE; // 分類所屬的類名 struct objc_method_list *instance_methods OBJC2_UNAVAILABLE; // 實例方法列表 struct objc_method_list *class_methods OBJC2_UNAVAILABLE; // 類方法列表 struct objc_protocol_list *protocols OBJC2_UNAVAILABLE; // 分類所實現的協議列表 }
通過上面我們可以發現,這個結構體主要包含了分類定義的實例方法與類方法,其中instance_methods 列表是 objc_class 中方法列表的一個子集,而class_methods列表是元類方法列表的一個子集。 但這個結構體里面 根本沒有屬性列表, 根本沒有屬性列表, 根本沒有屬性列表。
那么問題來了:
為什么在分類中聲明屬性時,運行不會出錯呢?
既然分類不讓添加屬性,那為什么我寫了@property仍然還以編譯通過呢?
接下來我們探究下分類不能添加屬性的實質原因:
我們知道在一個類中用@property聲明屬性,編譯器會自動幫我們生成_成員變量和setter/getter,但分類的指針結構體中,根本沒有屬性列表。所以在分類中用@property聲明屬性,既無法生成_成員變量也無法生成setter/getter。
因此結論是:我們可以用@property聲明屬性,編譯和運行都會通過,只要不使用程序也不會崩潰。但如果調用了_成員變量和setter/getter方法,報錯就在所難免了。
報錯原因如下:
// programmer.nameWithoutSetterGetter = @"無setter/getter"; //調用setter,編譯成功,運行報錯為:(-[Programmer setNameWithSetterGetter:]: unrecognized selector sent to instance 0x7f9de358fd70') // NSLog(@"%@",programmer.nameWithoutSetterGetter); //調用getter,編譯成功,運行報錯為-[Programmer setNameWithSetterGetter:]: unrecognized selector sent to instance 0x7fe22be11ea0' // NSLog(@"%@",_nameWithoutSetterGetter); //這是調用_成員變量,錯誤提示為:(Use of undeclared identifier '_nameWithoutSetterGetter')
那接下來我們繼續思考:
既然報錯的根本原因是使用了系統沒有生成的setter/getter方法,可不可以在手動添加setter/getter來避免崩潰,完成調用呢?
其實是可以的。由於OC是動態語言,方法真正的實現是通過runtime完成的,雖然系統不給我們生成setter/getter,但我們可以通過runtime手動添加setter/getter方法。那具體怎么實現呢?
代碼實現如下:
按照這個思路,我們通過運行時手動添加這個方法。
#import <objc/runtime.h>
static NSString *nameWithSetterGetterKey = @"nameWithSetterGetterKey"; //定義一個key值
@implementation Programmer (Category)
//運行時實現setter方法
- (void)setNameWithSetterGetter:(NSString *)nameWithSetterGetter {
objc_setAssociatedObject(self, &nameWithSetterGetterKey, nameWithSetterGetter, OBJC_ASSOCIATION_COPY);
}
//運行時實現getter方法
- (NSString *)nameWithSetterGetter {
return objc_getAssociatedObject(self, &nameWithSetterGetterKey);
}
@end
實際使用效果
//通過runtime實現了setter/getter
programmer.nameWithSetterGetter = @"Baby"; //調用setter,成功
NSLog(@"%@",programmer.nameWithSetterGetter); //調用getter,成功
//NSLog(@"%@",_nameWithSetterGetter); //這是調用_成員變量,錯誤提示為:(Use of undeclared identifier '_nameWithSetterGetter')
問題解決。但是注意,以上代碼僅僅是手動實現了setter/getter方法,但調用_成員變量依然報錯。
類擴展(Class Extension)
Extension是Category的一個特例。類擴展與分類相比只少了分類的名稱,所以稱之為“匿名分類”。
其實開發當中,我們幾乎天天在使用。對於有些人來說像是最熟悉的陌生人。
類擴展格式:
@interface XXX ()
//私有屬性
//私有方法(如果不實現,編譯時會報警,Method definition for 'XXX' not found)
@end
作用:
為一個類添加額外的原來沒有變量,方法和屬性
一般的類擴展寫到.m文件中
一般的私有屬性寫到.m文件中的類擴展中
類別與類擴展的區別:
runtime解決無
setter/getter的問題而已);
②類擴展不僅可以增加方法,還可以增加實例變量(或者屬性),只是該實例變量默認是@private類型的(
注意:
1.分類是用於給原有類添加方法的,因為分類的結構體指針中,沒有屬性列表,只有方法列表。所以< 原則上講它只能添加方法, 不能添加屬性(成員變量),實際上可以通過其它方式添加屬性> ;
2.分類中的可以寫@property, 但不會生成
setter/getter方法, 也不會生成實現以及私有的成員變量(編譯時會報警告);
3.可以在分類中訪問原有類中.h中的屬性;
4.如果分類中有和原有類同名的方法, 會優先調用分類中的方法, 就是說會忽略原有類的方法。所以同名方法調用的優先級為
分類 > 本類 > 父類。因此在開發中盡量不要覆蓋原有類;
