KVO
//添加观察者 、 回调、 移除观察者
[chinaMobile addObserver:per forKeyPath:@"fee"
options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil ];
chinaMobile.fee = 19.9;
// [chinaMobile removeObserver:per forKeyPath:@"fee"];
person.m文件下
- ( void )trafficChange{
NSLog(@"%i",_chinaMobile.traffic);
_chinaMobile.traffic--;
if (_chinaMobile.traffic == 490) {
NSLog(@"你的流量还剩490");
}
}
- ( void )setChinaMobile:(Chinamobile *)chinaMobile{
_chinaMobile = chinaMobile;
[self.chinaMobile addObserver:self forKeyPath:@"traffic"options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
}
- ( void )observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSLog(@"开始回调");
}
- ( void )dealloc{
[self.chinaMobile removeObserver:self forKeyPath:@"traffic"];
}
- ( id )init{
self = [super init];
if (self) {
NSLog(@"对象创建");
NSLog(@"%i",_chinaMobile.traffic);
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(trafficChange)userInfo:nil repeats:YES];
}
return self;
}
@end
代理模式 :
// main.m文件下
// 实例化一个病人
Sick *sick = [[Sick alloc]init];
sick.name = @"Jack";
// 找医生
Doctor *doctor = [[Doctor alloc]init];
doctor.name = @"Rose";
sick.delegate = doctor;
[sick ill];
// Sick.h文件下
@property(nonatomic,copy)NSString *name;
@property(nonatomic,strong)id <SickDelegate>delegate;
- ( void )ill;
// Sick.m文件下
- ( void )ill{
[self.delegate cure:self];
}
// Doctor.h文件下
@interface Doctor : NSObject<SickDelegate>
@property(nonatomic,copy)NSString *name;
// Doctor.m文件下
- ( void )cure:(Sick *)sick{
NSLog(@"%@医生给%@病人治疗",_name,sick.name);
}
// SickDelegate.h文件下
@class Sick;
@protocol SickDelegate <NSObject>
- ( void )cure:(Sick *)sick;
文件管理:
#import <Foundation/Foundation.h>
#define path @"/Users/feifanit/desktop/test.txt"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *string = @"Hello,World!你好!";
NSData * data = [string dataUsingEncoding:NSUTF8StringEncoding];
// NSString * readStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSFileHandle * fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSData * readData = fileHandle.availableData;
NSString * readStr = [[NSString alloc]initWithData:readData encoding:NSUTF8StringEncoding];
NSLog(@"readSTR%@",readStr);
//在文件内容的末尾写
NSFileHandle *fh = [NSFileHandle fileHandleForUpdatingAtPath:path];
[fh seekToEndOfFile];
//写
[fh writeData:data];
//关闭
[fh closeFile];
//归档解档
//归档
NSString *stringII = @"hello,world";
NSArray * array = @[@1,@2,@3,@4,@5];
NSDictionary *dict = @{@"name": @"baby"};
NSMutableData *muData = [[NSMutableData alloc]init];
//实例化归档数据
NSKeyedArchiver *arch = [[NSKeyedArchiver alloc]initForWritingWithMutableData:muData];
//归档对象
[arch encodeObject:stringII forKey:@"stringII"];
[arch encodeObject:array forKey:@"array"];
[arch encodeObject:dict forKey:@"dict"];
//完成归档
[arch finishEncoding];
//把数据流写到文件中
[muData writeToFile:@"/Users/feifanit/desktop/dict.plist" atomically:YES];
//解档
//用一个NSData 去接收解档对象
NSData *dataI = [[NSData alloc]initWithContentsOfFile:@"/Users/feifanit/desktop/dict.plist"];
//解档对象
NSKeyedUnarchiver *unarc =[[NSKeyedUnarchiver alloc]initForReadingWithData:dataI];
//解码
id unstring = [ unarc decodeObjectForKey:@"stringII"];
id unarray = [ unarc decodeObjectForKey:@"array"];
id undit = [unarc decodeObjectForKey:@"dict"];
NSLog(@"%@ %@ %@",unstring,unarray,undit);
}
return 0;
}