多值参数和中文输出

多值参数

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

    -(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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在开发当中,不免会遇到需要处理一些多值参数的情况,或者从后台拿到的数据输出中文的情况,接下来,我们详细讲解下,在开...
    小白文_Vincent阅读 375评论 0 0
  • JSON数据解析: JSON的简单介绍:什么是JSONJSON以一种轻量级的数据格式,一般用来数据交互服务器返回给...
    木子尚武阅读 307评论 0 0
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 1,720评论 0 3
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    FF_911阅读 1,424评论 0 3
  • 昨天幸福实修三天课程最后的狂欢也结束了。我内心很喜悦的同时,觉得进到这里来实修的伙伴们其实都是勇敢的伙伴,进来后我...
    嘟嘟妈妈2727阅读 236评论 1 0