KVC/KVO与AFNetworking

 

KCV/KVOAFNetworking


一.KVC/KVO


1.KVC的使用


      KVC全称是Key Value Coding(键值对编程),其本质上是对NSObject类的扩展(NSObject类的Category),它提供一种机制来间接访问对象的属性。KVO全称Key Value Observing(键值对观察),是建立KVC之上的模型对象的观察者模式的实现。

  一个对象拥有某些属性。比如说,一个Person对象有一个name和一个address属性。以KVC说法,Person对象分别有一个value对应他的name和address键。 key只是一个字符串,它对应的值可以是任意类型的对象。从最基础的层次上看,KVC有两个方法:一个是设置key的值,另一个是获取key对应的值。

  例如我们从网络获得一个JSON格式的数据,它里面的键值对正好跟一个对象的属性一一对应,那么我们可以非常方便的用JSON格式的数据完成对象属性的赋值,而不需要去处理对象的每一个属性,代码如下所示:


#import <UIKit/UIKit.h>


@interface Person : NSObject


@property (nonatomic, copy) NSString *name;

@property (nonatomic, assign) int age;

@property (nonatomic, assign) double weight;


@end

#import "Person.h"


@implementation Person


- (void)setValue:(id)value forUndefinedKey:(NSString *)key {

    // 遇到没有对应的键时什么都不要做,这一点非常重要

}


- (NSString *)description {

    return [NSString stringWithFormat:@"%@ - %d - %f", self.name, self.age, self.weight];

}


@end

- (void)viewDidLoad {

    [super viewDidLoad];

    

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    [dict setObject:@“elean” forKey:@"name"];

    [dict setObject:@“18” forKey:@"age"];

    [dict setObject:@“40” forKey:@"weight"];

    

    Person *person = [[CDPerson alloc] init];

    // 直接用字典中的键值对映射给person的对应属性赋值

    [person setValuesForKeysWithDictionary:dict];

    // 通过KVC的方式修改对象的属性

    [person setValue:@"张三丰" forKey:@"name"];

    [person setValue:@"120" forKey:@"age"];

    

    NSLog(@"%@", person);

}



2.KVO的使用


      Key-Value Observing(KVO)建立在KVC之上,它能够观察一个对象指定属性值的变化,在需要的时候产生对应的通知。


KVO示例代码:

#import <Foundation/Foundation.h>


@interface Account : NSObject 


@property (nonatomic, assign) double balance;


@end


@implementation Account


@end


@interface Person : NSObject 


@property (nonatomic, strong) NSString *name;


@property (nonatomic, assign) int age;


@property (nonatomic, strong) Account *account;


@end


@implementation Person


- (void)setAccount:(Account *)account {

    _account = account;

    [self.account addObserver:self forKeyPath:@"balance"

                      options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    if ([keyPath isEqualToString:@"balance"]) {

        //Account *tempAccount = (id) object;

        //NSLog(@"账户余额发生改变: %f", tempAccount.balance);;

        double oldValue = [change[@"old"] doubleValue];

        double newValue = [change[@"new"] doubleValue];

        NSLog(@"账户余额改变: %f", newValue - oldValue);

    }

}


- (NSString *)description {

    return [NSString stringWithFormat:@"%@ : %d : %f", self.name, self.age, self.account.balance];

}


- (void)dealloc {

    [self.account removeObserver:self forKeyPath:@"balance"];

}


@end


int main(int argc, const char * argv[]) {

    @autoreleasepool {

        Person *person = [[Person alloc] init];

        person.name = @“elean”;

        person.age = 18;

        Account *account = [[Account alloc] init];

        account.balance = 1000;

        person.account = account;

        

        NSLog(@"%@", person);

        

        person.account.balance = 800;

    }

    return 0;

}



这里有几点需要注意:

  • valueForKey:方法用于以字符串调用对象的get属性方法,或者读取成员变量的值;与之相对的是setValue:forKey:,它用于以字符串调用对象的set属性方法,或者修改成员变量的值。


  • 对于基本数据类型,KVC方法会对基本数据类型进行封装,基本数据类型封装为NSNumber,其他结构体类型封装为NSValue


  • 在使用KVC时,如果找不到字符串对应的属性和成员变量时会调用valueForUndefinedKey:或者setValue:forUndefinedKey:这两个方法,默认情况下会抛出异常


  • 在默认情况下KVC方法能够直接访问类的私有成员变量,如果我们不想这样,可以重写accessInstanceVariablesDirectly方法,并令其返回NO(默认是返回YES)


  • KVC方法定义在NSKeyValueCoding类别中,该类别附加于NSObject类上,所以所有对象都具有这些方法


  • 在一些特殊的类的对象上调用KVC方法会有特别的效果。对于数组NSArray、集合NSSet,调用valueForKey:会对每个数组和集合成员调用valueForKey:,并返回新的数组或者集合


  • 在KVC中还有一种常用技术,称为键值链(Key Path)。(拓展内容  详情查看代码)键值链是用点将若干键相连的字符串,例如“manufacturer.product.name”。通过在对象上调用valueForKeyPath:或者setValue:forKeyPath:,我们就可以在一条语句中级联调用指定的属性。



二.JSONModel

      JSONModel可以让你快速创建智能的JSON数据模型,可在iOS和MacOS X应用中使用。使用JSONModel时,不需要额外去检查所要的服务器属性是否有返回。JSONModel的initWithDictionary方法会自动去进行检查并处理。


json.png






三.SDWebImage图片的异步下载


 [SDWebImage下载地址链接](https://github.com/rs/SDWebImage)


 [SDWebImage的文档连接](http://cocoadocs.org/docsets/SDWebImage/3.7.2/)


旧版本的SDWebImage使用MRC编写  需要在工程-》Build Phases-》Compile Sources-》SDWebImage相关文件后 输入 -fno-objc-arc






四.AFNetworking

        AFNetworking是一个能够快速使用的iOS和MacOS X下的网络框架,它是构建在Foundation URL Loading System之上的,封装了网络的抽象层,可以方便的使用,是一个模块化架构并拥有丰富API的框架。


HTTP请求和操作

// GET请求

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Error: %@", error);

}];




// 带有表单参数的POST请求

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

NSDictionary *parameters = @{@"foo": @"bar"};

[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Error: %@", error);

}];





// 上传表单和文件

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

NSDictionary *parameters = @{@"foo": @"bar"};

NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];

[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    [formData appendPartWithFileURL:filePath name:@"image" error:nil];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"Success: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Error: %@", error);

}]; 



Session管理


// 创建一个下载任务

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];


NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];

NSURLRequest *request = [NSURLRequest requestWithURL:URL];


NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];

} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

    NSLog(@"File downloaded to: %@", filePath);

}];

[downloadTask resume];



// 创建一个上传任务

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];


NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];

NSURLRequest *request = [NSURLRequest requestWithURL:URL];


NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {

    if (error) {

        NSLog(@"Error: %@", error);

    } else {

        NSLog(@"Success: %@ %@", response, responseObject);

    }

}];

[uploadTask resume];





// 创建一个带进度的上传任务

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];

    } error:nil];


AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSProgress *progress = nil;


NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {

    if (error) {

        NSLog(@"Error: %@", error);

    } else {

        NSLog(@"%@ %@", response, responseObject);

    }

}];


[uploadTask resume];



// 创建请求对象

NSString *URLString = @"http://example.com";

NSDictionary *parameters = @{@"foo": @"bar", @"baz": @[@1, @2, @3]};

[[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:URLString parameters:parameters error:nil];

// 网络可达性检测

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

    NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));

}];




[[AFNetworkReachabilityManager sharedManager] startMonitoring];

// 基于HTTP请求操作管理器的网络可达性检测

NSURL *baseURL = [NSURL URLWithString:@"http://example.com/"];

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];


NSOperationQueue *operationQueue = manager.operationQueue;

[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

    switch (status) {

        case AFNetworkReachabilityStatusReachableViaWWAN:

        case AFNetworkReachabilityStatusReachableViaWiFi:

            [operationQueue setSuspended:NO];

            break;

        case AFNetworkReachabilityStatusNotReachable:

        default:

            [operationQueue setSuspended:YES];

            break;

    }

}];


[manager.reachabilityManager startMonitoring];




// 批量任务处理

NSMutableArray *mutableOperations = [NSMutableArray array];

for (NSURL *fileURL in filesToUpload) {

    NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];

    }];


    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];


    [mutableOperations addObject:operation];

}


NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {

    NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);

} completionBlock:^(NSArray *operations) {

    NSLog(@"All operations in batch complete");

}];

[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];



<key>NSAppTransportSecurity</key>

<dict>

<key>NSAllowsArbitraryLoads</key>

<true/>

</dict>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值