多值参数和中文输出

2016-10-19  本文已影响20人  Mario_ZJ

多值参数

如果一个参数对应着多个值,那么直接按照"参数=值&参数=值"的方式拼接

    -(void)test
    {
        //1.确定URL
        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/weather?place=Beijing&place=Guangzhou"];
        //2.创建请求对象
        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        //3.发送请求
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

            //4.解析
            NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
        }];
    }

中文输出

#import "NSDictionary+log.h"
@implementation NSDictionary (log)
//重写字典的descriptionWithLocale:indent:方法,实现中文的输出
-(NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
{
    //创建一个可变字符串,用于拼接输出的内容
    NSMutableString * strM = [NSMutableString string];
    [strM appendString:@"\t{\n"];
    //使用迭代法遍历字典中的键值对
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        [strM appendFormat:@"\t%@:",key];
        [strM appendFormat:@"%@,\n",obj];
    }];
    [strM appendString:@"\t}"];
    //删除最后一个逗号
    NSRange range = [strM rangeOfString:@"," options:NSBackwardsSearch];
    if (range.location != NSNotFound) {
        [strM deleteCharactersInRange:range];
    }
    return strM;
}
@end
@implementation NSArray (log)
//重写数组的descriptionWithLocale:indent方法
-(NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
{
    NSMutableString * strM = [NSMutableString string];
    [strM appendString:@"\t[\n"];
    //用迭代法遍历数组
    [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [strM appendFormat:@"%@,\n",obj];
    }];
    [strM appendString:@"\t]"];
    //删除最后一个逗号
    NSRange range = [strM rangeOfString:@"," options:NSBackwardsSearch];
    if (range.location != NSNotFound) {
        [strM deleteCharactersInRange:range];
    }
    return strM;
}
@end
上一篇下一篇

猜你喜欢

热点阅读