{"id":689,"date":"2023-11-22T09:01:01","date_gmt":"2023-11-22T01:01:01","guid":{"rendered":""},"modified":"2023-11-22T09:01:01","modified_gmt":"2023-11-22T01:01:01","slug":"\u4f7f\u7528 NSURLComponents \u548c NSURLQueryItem \u6765\u8fdb\u884c\u66f4\u89c4\u8303\u7684 URL \u5b57\u7b26\u4e32\u62fc\u63a5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/689.html","title":{"rendered":"\u4f7f\u7528 NSURLComponents \u548c NSURLQueryItem \u6765\u8fdb\u884c\u66f4\u89c4\u8303\u7684 URL \u5b57\u7b26\u4e32\u62fc\u63a5"},"content":{"rendered":"

\u5728\u65e5\u5e38\u7684\u7f16\u7a0b\u5de5\u4f5c\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u62fc\u63a5URL\u5b57\u7b26\u4e32\uff0c\u53ef\u80fd\u76f4\u63a5\u5199\u6210\u5982\u4e0b\u7684\u5f62\u5f0f\uff1a<\/p>\n

NSString<\/span> *urlStr = @\"https:\/\/www.demo.com\/query?name=Jack\"<\/span>;\n<\/code><\/pre>\n

\u5bf9\u4e8e\u6bd4\u8f83\u7b80\u77ed\u7684url\u5b57\u7b26\u4e32\uff0c\u8fd9\u79cd\u5199\u6cd5\u76f4\u89c2\u65b9\u4fbf\u3002\u4f46\u662f\u5982\u679c\u9700\u8981\u62fc\u63a5\u4e00\u5927\u4e32\u7684query\u8bed\u53e5\u7684\u8bdd\uff0c\u5c31\u4f1a\u975e\u5e38\u5730\u957f\uff08\u867d\u7136Objective-C\u8bed\u6cd5\u672c\u8eab\u4e5f\u662f\u975e\u5e38\u7684\u957f\uff09\uff0c\u8b6c\u5982:<\/p>\n

NSString<\/span> *urlStr = \n[NSString<\/span> stringWithFormat:@\"https:\/\/www.demo.com\/query?name=%@&gender=%@&lng=%@&lat=%@&age=%@\"<\/span>, @\"Jack\"<\/span>, @\"male\"<\/span>, @\"121\"<\/span>, @\"31\"<\/span>, @\"20\"<\/span>];\n<\/code><\/pre>\n

\u6b64\u65f6\u5982\u679c\u4f60\u7684\u663e\u793a\u5668\u4e0d\u662f\u7279\u522b\u5bbd\u7684\u8bdd\uff0c\u5e94\u8be5\u5df2\u7ecf\u81ea\u52a8\u6362\u884c\u4e86\uff0c\u5e76\u4e14\u5360\u4f4d\u7b26\u4e5f\u6bd4\u8f83\u591a\uff0c\u770b\u8d77\u6765\u6bd4\u8f83\u4e71\uff0c\u540e\u7eed\u7ef4\u62a4\u4e5f\u4f1a\u6bd4\u8f83\u9ebb\u70e6\u3002 \u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528NSURLComponents<\/code>\u548cNSURLQueryItem<\/code>\u6765\u8fdb\u884c\u62fc\u63a5:<\/p>\n

NSURLComponents<\/span> *components = [NSURLComponents<\/span> componentsWithString:@\"https:\/\/www.demo.com\/query\"<\/span>];\nNSMutableArray<\/span><NSURLQueryItem<\/span> *> *queryItems = @[].mutableCopy;\nNSURLQueryItem<\/span> *nameItem = [NSURLQueryItem<\/span> queryItemWithName:@\"name\"<\/span> value:@\"Jack\"<\/span>];\nNSURLQueryItem<\/span> *genderItem = [NSURLQueryItem<\/span> queryItemWithName:@\"gender\"<\/span> value:@\"male\"<\/span>];\nNSURLQueryItem<\/span> *lngItem = [NSURLQueryItem<\/span> queryItemWithName:@\"lng\"<\/span> value:@\"121\"<\/span>];\nNSURLQueryItem<\/span> *latItem = [NSURLQueryItem<\/span> queryItemWithName:@\"lat\"<\/span> value:@\"31\"<\/span>];\nNSURLQueryItem<\/span> *ageItem = [NSURLQueryItem<\/span> queryItemWithName:@\"age\"<\/span> value:@\"20\"<\/span>];\n[queryItems addObject:nameItem];\n[queryItems addObject:genderItem];\n[queryItems addObject:lngItem];\n[queryItems addObject:latItem];\n[queryItems addObject:ageItem];\ncomponents.queryItems = queryItems;\n\nNSURL<\/span> *url = components.URL;\n...\n\n<\/code><\/pre>\n

\u8fd9\u6837\u5199\u5c31\u6bd4\u8f83\u6e05\u6670\uff0c\u5e76\u4e14\u540e\u671f\u7ef4\u62a4\u8d77\u6765\u4e5f\u76f8\u5bf9\u5bb9\u6613\u4e9b\uff0c\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u7528\u5b57\u5178\u7b80\u5355\u5c01\u88c5\u4e00\u4e0bNSURLQueryItem<\/code>\u6570\u7ec4(\u5728\u7edd\u5927\u591a\u6570\u7684\u573a\u666f\u4e2d\u9002\u7528)\uff1a<\/p>\n

- (NSArray<\/span><NSURLQueryItem<\/span> *> *)queryItemsWithParameters:(NSDictionary<\/span><NSString<\/span> *, NSString<\/span> *> *)dict {\n    if<\/span> ([dict isKindOfClass:[NSDictionary<\/span> class<\/span>]] && [dict count] > 0<\/span>) {\n\n        NSMutableArray<\/span><NSURLQueryItem<\/span> *> *arrM = @[].mutableCopy;\n        for<\/span> (NSString<\/span> *keyStr in<\/span> dict.allKeys) {\n            NSURLQueryItem<\/span> *item = [NSURLQueryItem<\/span> queryItemWithName:keyStr value:dict[keyStr]];\n            [arrM addObject:item];\n        }\n        return<\/span> arrM;\n    }\n\n    return<\/span> nil<\/span>;\n}\n\n\n\n    NSDictionary<\/span> *dict = @{@\"name\"<\/span>: @\"Jack\"<\/span>,\n                           @\"gender\"<\/span>: @\"male\"<\/span>,\n                           @\"lng\"<\/span>: @\"121\"<\/span>,\n                           @\"lat\"<\/span>: @\"31\"<\/span>,\n                           @\"age\"<\/span>: @\"20\"<\/span>};\n                    \n    components.queryItems = [self<\/span> queryItemsWithParameters:dict];                           \n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528 NSURLComponents \u548c NSURLQueryItem \u6765\u8fdb\u884c\u66f4\u89c4\u8303\u7684 URL \u5b57\u7b26\u4e32\u62fc\u63a5\u5bf9\u4e8e\u6bd4\u8f83\u7b80\u77ed\u7684url\u5b57\u7b26\u4e32\uff0c\u8fd9\u79cd\u5199\u6cd5\u76f4\u89c2\u65b9\u4fbf\u3002\u4f46\u662f\u5982...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/689"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=689"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/689\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}