网络知识点(一)

网络02

  1. 网络请求的两种形式

第一种不能设置网络请求的信息(缓存策略,超时时间,可以放在请求头里面)

常见的缓存策略

默认

     NSURLRequestUseProtocolCachePolicy = 0,

     忽略本地的缓存

     NSURLRequestReloadIgnoringLocalCacheData = 1,

     NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,

     返回数据如果没有缓存就去加载

     NSURLRequestReturnCacheDataElseLoad = 2,

     返回数据如果没有缓存就不去加载

     NSURLRequestReturnCacheDataDontLoad = 3,

NSData 简单同步的方式获取数据,但是不能自定义请求等信息//    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/demo.json"];

NSData *data = [NSData dataWithContentsOfURL:url];

NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@",string);

第二种请求方式

SURL *url = [NSURL URLWithString:@"http://127.0.0.12/demo.json"]

//设置请求头信息,请求必须使用可变的

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:3];

    //User-Agent: iPhone

    [request setValue:@"iPhone" forHTTPHeaderField:@"User-Agent"];

       [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

                if(connectionError){

            NSLog(@"conn error");

            return;

        }

                NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"%@",string);

            }];

2.常见的json解析

  NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/demo.json"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

   //网线连接错误

        if(connectionError){

            NSLog(@"conn error");

            return;

        }

//服务器的错误

    //判断服务器是否有错,状态码

        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

        if(httpResponse.statusCode == 200 || httpResponse.statusCode == 304){

        //成功

//            NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

//            NSLog(@"%@",string);

//网络上传世的都是二进制,反序列化就是从二进制转变成对象

id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

            NSLog(@"%@",obj);

                    }else{

            NSLog(@"服务器内部错误");

        }

    }];

3.将乱码转化为中文

(导入一个类扩展文件)#import "NSArray+Log.h"

文件的具体内容

@implementation NSArray (Log)

- (NSString *)descriptionWithLocale:(id)locale

{

    NSMutableString *strM = [NSMutableString stringWithString:@"( "];

        [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        [strM appendFormat:@" %@, ", obj];

    }];

        [strM appendString:@")"];

        return strM;

}

@end

@implementation NSDictionary (Log)

- (NSString *)descriptionWithLocale:(id)locale

{

    NSMutableString *strM = [NSMutableString stringWithString:@"{ "];

        [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

        [strM appendFormat:@" %@ = %@; ", key, obj];

    }];

    [strM appendString:@"} "];

        return strM;

}

@end

4.josnkit的使用

第一导入头文件#import "JSONKit.h"

第二:添加键值对

第三:id object =  [[JSONDecoder decoder] objectWithData:data];

5.重新写模型的desc方法

//<CZMessage: 0x7fc7c1522cc0>

-(NSString *)description{

    return [NSString stringWithFormat:@"%@ {message=%@,messageId=%d}",[super description],self.message,self.messageId.intValue];

}

 

6.plist的解析

//<CZMessage: 0x7fc7c1522cc0>

-(NSString *)description{

    return [NSString stringWithFormat:@"%@ {message=%@,messageId=%d}",[super description],self.message,self.messageId.intValue];

}

7. id object =  [NSPropertyListSerialization propertyListWithData:data options:0 format:0 error:NULL];

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

9.一般reload数据可以放在数值的set方法里面

10.遍历数组,采用block进行遍历

//遍历,字典转模型

            [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

                CZNews *news = [CZNews newsWithDictionary:obj];

                [mArray addObject:news];

            }];

11.是能够添加refresh Control的方法未找到

设置的信息refresh Control

//设置刷新的颜色

    [self.refreshControl setTintColor:[UIColor redColor]];

        //设置文字的颜色

    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"loading" attributes:@{NSForegroundColorAttributeName:[UIColor blueColor]}];

   

    [self.refreshControl setAttributedTitle:attrString];

在加载完数据的时候讲refresh Control去掉

[self.refreshControl endRefreshing];

12,使用SD的步骤

#import "UIImageView+WebCache.h"

[self.iv_img sd_setImageWithURL:[NSURL URLWithString:model.img]];

13. //Xcode7之后可能不会刷新,需要手动刷新

[self.lb_title layoutIfNeeded];

14. //判断title是否一行显示,如果超出一行,summary不显示

    CGFloat stringWidth = [self.model.title sizeWithAttributes:@{NSFontAttributeName:self.lb_title.font}].width;

    CGFloat labelWidth = self.lb_title.frame.size.width;

    if(stringWidth > labelWidth){

        self.lb_summary.hidden = YES;

    }else{

        self.lb_summary.hidden = NO;

}

15.关键掌握通过字符串获取长度

CGFloat stringWidth = [self.model.title sizeWithAttributes:@{NSFontAttributeName:self.lb_title.font}].width;

16.关于注册的问题

collectionView一般都是要注册的,除非使用原型的cell

nib以及使用class的注册,不用判断缓存池里面是否有cell

tableVIEW注册也不用判断cell缓存池

原型用长的方法

自定义class使用短的方法

 

原文地址:https://www.cnblogs.com/chaoyueME/p/5578877.html