多值參數(shù)
如果一個參數(shù)對應著多個值,那么直接按照"參數(shù)=值&參數(shù)=值"的方式拼接
-
多值參數(shù)的寫法
//錯誤的寫法: http://120.25.226.186:32812/weather?place=Beijing&guangzhou
//正確的寫法: http://120.25.226.186:32812/weather?place=Beijing&place=guangzhou
示例代碼:
-(void)test
{
//1.確定URL
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/weather?place=Beijing&place=Guangzhou"];
//2.創(chuàng)建請求對象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.發(fā)送請求
[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]);
}];
}
中文輸出
- 如何解決字典和數(shù)組中輸出亂碼的問題
給字典和數(shù)組添加一個分類灯谣,重寫descriptionWithLocale方法扁藕,在該方法中拼接元素格式化輸出削樊。
-(nonnull NSString *)descriptionWithLocale:(nullable id)locale
示例代碼:- 重寫字典的
descriptionWithLocale
方法
- 重寫字典的
#import "NSDictionary+log.h"
@implementation NSDictionary (log)
//重寫字典的descriptionWithLocale:indent:方法,實現(xiàn)中文的輸出
-(NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
{
//創(chuàng)建一個可變字符串,用于拼接輸出的內容
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
- 重寫數(shù)組的
descriptionWithLocale
方法
@implementation NSArray (log)
//重寫數(shù)組的descriptionWithLocale:indent方法
-(NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
{
NSMutableString * strM = [NSMutableString string];
[strM appendString:@"\t[\n"];
//用迭代法遍歷數(shù)組
[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