美文网首页
多值参数和中文输出

多值参数和中文输出

作者: Mario_ZJ | 来源:发表于2016-10-19 15:19 被阅读20次

    多值参数

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

        -(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]);
            }];
        }
    

    中文输出

    • 如何解决字典和数组中输出乱码的问题
      给字典和数组添加一个分类,重写descriptionWithLocale方法,在该方法中拼接元素格式化输出。
      -(nonnull NSString *)descriptionWithLocale:(nullable id)locale
      示例代码:
      • 重写字典的descriptionWithLocale方法
    #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
    
    • 重写数组的descriptionWithLocale方法
    @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
    

    相关文章

      网友评论

          本文标题:多值参数和中文输出

          本文链接:https://www.haomeiwen.com/subject/jyhvettx.html