oc中KVO 、代理模式、文件管理

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;
}

转载于:https://my.oschina.net/u/2555813/blog/600433

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值