1.如題呈驶,比如這樣,將基本要素等聲明放在主實(shí)現(xiàn)中城瞎,執(zhí)行不同的操作所用的另外幾套方法則歸入各個(gè)分類中竖共。
頭文件
@interface Person : NSObject <NSCopying>
@property (nonatomic,copy) NSString *name;
@property (nonatomic,readonly) NSArray *friends;
@property (nonatomic,assign) int age;
@end
@interface Person (FriendShip)
- (void)addFriend:(Person *)person;
- (void)removeFriend:(Person *)person;
@end
@interface Person (Play)
- (void)playPingPong;
- (void)playFootball;
- (void)sing;
- (void)run;
@end
實(shí)現(xiàn)文件
@interface Person ()
@property (nonatomic,readwrite,strong) NSMutableArray *friends;
@end
@implementation Person
- (instancetype)initWithName:(NSString *)name age:(int)age
{
self = [super init];
if (self) {
self.name = name;
self.age = age;
_friends = [NSMutableArray array];
}
return self;
}
- (NSArray *)friends{
return [_friends copy];
}
- (id)copyWithZone:(NSZone *)zone{
Person *p = [[[self class] allocWithZone:zone] initWithName:_name age:_age];
p->_friends = [_friends mutableCopy];
return p;
}
@end
@implementation Person (FriendShip)
- (void)addFriend:(Person *)person{
if(person){
[_friends addObject:person];
}
}
- (void)removeFriend:(Person *)person{
if(person){
[_friends removeObject:person];
}
}
@end
@implementation Person (Play)
- (void)playPingPong {
}
- (void)playFootball {
}
- (void)sing{
}
- (void)run{
}
- (void)eat{
}
@end
2.這樣的好處
- 易于管理苗胀,方便檢視
-
分類名稱會(huì)出現(xiàn)在符號(hào)信息中
3.將視為私有的方法歸入名叫private的分類中狡刘,以隱藏其細(xì)節(jié)享潜。
- 在編寫分享給其他開發(fā)者使用的程序庫時(shí),可以考慮創(chuàng)建Private的分類嗅蔬。經(jīng)常會(huì)遇到這樣的一些方法:他們不是公共API的一部分,然而卻非常適合在程序庫之內(nèi)使用剑按。此時(shí)應(yīng)該創(chuàng)建Private分類,如果程序庫中某個(gè)地方要用到這些方法澜术,就引入這個(gè)分類的頭文件艺蝴。而分類的頭文件并不隨程序庫一并公開,于是該庫的使用者并不知道這些方法鸟废。
- 如果調(diào)用者通過其他方式調(diào)用猜敢,在調(diào)試器中看到Private一詞,便知道不能直接調(diào)用。