@interface Person (Play)
- (void)pGotoTheCinema;
- (void)pGotoSportGame;
@end
#import "Person+Play.h"
@implementation Person (Play)
- (void)pGotoTheCinema{
}
- (void)pGotoSportGame{
}
@end
Class-continuation类型的Category
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,copy,readonly) NSString *firstName;
@property (nonatomic,copy,readonly) NSString *lastName;
@property (nonatomic,strong,readonly) NSArray *friends;
- (instancetype)initWithFristName:(NSString *)firstName withLastName:(NSString *)lastName;
@end
--------------------
#import "Person.h"
@interface Person()
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,strong,readwrite) NSArray *friends;
@end
@implementation Person
- (instancetype)initWithFristName:(NSString *)firstName withLastName:(NSString *)lastName{
self = [super init];
if (self) {
_firstName = firstName;
_lastName = lastName;
}
return self;
}
@end
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,copy,readonly) NSString *firstName;
@property (nonatomic,copy,readonly) NSString *lastName;
@property (nonatomic,strong,readonly) NSArray *friends;
- (instancetype)initWithFristName:(NSString *)firstName withLastName:(NSString *)lastName;
@end
-------------------------------
#import "Person.h"
@interface Person()
@property (nonatomic,strong,readwrite) NSArray *friends;
@end
@implementation Person
- (instancetype)initWithFristName:(NSString *)firstName withLastName:(NSString *)lastName{
self = [super init];
if (self) {
_firstName = firstName;
_lastName = lastName;
}
return self;
}
@end
#import <Foundation/Foundation.h>
@class User;
@interface APP : NSObject
@property (nonatomic,strong) User *user;
@end
-----------------------------
#import "APP.h"
#import "User.h"
@interface APP()<UserDelegate>
@end
@implementation APP
-(void)doSomething{
NSLog(@"dosomething");
}
@end
普通类型的Category
#import "Person.h"
@interface Person (Play)
- (void)pGotoTheCinema;
- (void)pGotoSportGame;
@end
--------------
#import "Person+Play.h"
@implementation Person (Play)
- (void)pGotoTheCinema{
}
- (void)pGotoSportGame{
}
@end
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,copy) NSString *firstName;
@property (nonatomic,copy) NSString *lastName;
@property (nonatomic,strong) NSArray *friendArray;
- (instancetype)initWithFristName:(NSString *)firstName withLastName:(NSString *)lastName;
- (void)addPerson:(Person *)person;
- (void)removePerson:(Person *)person;
- (BOOL)isFriendWithPerson:(Person *)person;
- (void)performDayWork;
- (void)takeVacationFromWork;
- (void)gotoTheCinema;
- (void)gotoSportGame;
@end
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,copy) NSString *firstName;
@property (nonatomic,copy) NSString *lastName;
@property (nonatomic,strong) NSArray *friends;
- (instancetype)initWithFristName:(NSString *)firstName withLastName:(NSString *)lastName;
@end
-------------------------
#import "Person.h"
@interface Person (Work)
- (void)wPerformDayWork;
- (void)wTakeVacationFromWork;
@end
-------------------------
#import "Person.h"
@interface Person (Play)
- (void)pGotoTheCinema;
- (void)pGotoSportGame;
@end
-------------------------
#import "Person.h"
@interface Person (Friendship)
- (void)fAddPerson:(Person *)person;
- (void)fRemovePerson:(Person *)person;
- (BOOL)fIsFriendWithPerson:(Person *)person;
@end
#import "Person.h"
@interface Person (Play)
@property (nonatomic,copy) NSString *playGameName;
- (void)pGotoTheCinema;
- (void)pGotoSportGame;
@end
-------------
- (void)viewDidLoad {
[super viewDidLoad];
Person *p = [[Person alloc]initWithFristName:@"firstName" withLastName:@"lastName"];
p.playGameName = @"football";
}
Xcode会有警告
Xcode会有警告.png
运行程序会崩溃
运行程序会崩溃.png
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,copy,readonly) NSString *firstName;
@property (nonatomic,copy,readonly) NSString *lastName;
@property (nonatomic,strong,readonly) NSArray *friends;
- (instancetype)initWithFristName:(NSString *)firstName withLastName:(NSString *)lastName;
- (void)run;
@end
------------------------------------
#import "Person.h"
@interface Person (Play)
@property (nonatomic,copy) NSString *playGameName;
- (void)pGotoTheCinema;
- (void)pGotoSportGame;
- (void)run;
@end
-------------------------------------
#import "Person.h"
@interface Person (Work)
- (void)wPerformDayWork;
- (void)wTakeVacationFromWork;
- (void)run;
@end
-------------------------------------
Person *p = [[Person alloc]initWithFristName:@"firstName" withLastName:@"lastName"];
[p run];
log.png