用OBJC編程 4 - Customizing Existing Classes
通過Category為類增加方法#import "XYZPerson.h"
@interface XYZPerson (XYZPersonNameDisplayAddtions)
- (NSString *)lastNameFirstNameString;
@end
// ----------------------
#import "XYZPerson+XYZPersonNameDisplayAddtions.h"
@implementation XYZPerson (XYZPersonNameDisplayAddtions)
- (NSString *)lastNameFirstNameString{
return [NSString stringWithFormat:@"%@ %@", self.lastName, self.firstName];
}
@end
//-------------------------------
#import "XYZPerson+XYZPersonNameDisplayAddtions.h"
@implementation SomeObject
-(void) someMethod{
XYZPerson *person = [[XYZPerson alloc] initWithFirstName:@"John", lastName:@"Doe"];
NSLog(@"The people is %@", [person lastNameFirstNameString]);
}
@end
Category 可以增加任何的實例方法和類方法,但是通常不適合增加Property,雖然語法上可以聲明一個Property,但不能通過Category增加一個實例變量。這意味著不能synthesize任何實例變量, 也沒有存取方法。 你可以寫自己的accessor,但是不能keep track property,它們存儲在原始的類里。避免名字沖突Category 的新增方法可能會導致名字沖突,在運行時可能會出現(xiàn)未知的行為,為了避免這種情況,需要增加前綴@interface NSSortDescriptor (XYZAdditions)
+ (id)xyz_sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending;
@end
// ------ use it -------
NSSortDescriptor *descriptor = [NSSortDescriptor xyz_sortDescriptorWithKey:@"name" ascending:YES];
類擴展類似分類,但只能用于編譯時有源碼情況,并且必須寫在實現(xiàn)文件里,因此不能通過這個為框架類擴展。擴展語法類似分類@interface ClassName ()
{
id _someCustomInstanceVariable;
}
@property NSObject *extraProperty;
@end
也叫匿名分類。不像分類那樣,擴展可以增加屬性和實例變量。編譯器會自動synthesize accessor方法。如果你增加方法,必須實現(xiàn)在主要的@implementation代碼塊里。可以用擴展來聲明私有屬性和方法@interface XYZPerson:NSObject
@proerty (readonly) NSString *uniqueIdentifier;
-(void)assignUniqueIdentifier;
@end
/// ---------------------
@interface XYZPerson ()
@property (readwrite) NSString *uniqueIdentifier;
@end
@implementation XYZPerson
// ,,,
@end
像上面那樣重復聲明uniqueIdentifier為readwrite,每一個運行時的XYZPerson對象都將存在一個setter。任何訪問私有方法,或者set一個readonly屬性的行為,編譯器都會產(chǎn)生錯誤,但是可以通過動態(tài)運行時特性避免編譯器錯誤,諸如調(diào)用NSObject的performSelector方法。如果你打算構(gòu)建私有方法或?qū)傩裕憧梢月暶饕粋€分離的頭文件來聲明擴展,諸如XYZPerson.h和XYZPersonPrivate.h分類和擴展并不是定制一個類的唯一途徑,也不是最好方法,要考慮可否使用子類化或者委托的方法來實現(xiàn)。