我參考這個答案來決議 url 但是在決議這個 url 時
NSString *urlString = @"https://www.example.com/product-detail?journey_id=123456&iswa=1";
我得到了我的第一把鑰匙,https://www.example.com/product-detail?journey_id但我只需要journey_id作為我的鑰匙。
這是我在編碼中所做的:
NSString *urlString = @"https://www.example.com/product-detail?journey_id=123456&iswa=1";
NSMutableDictionary *waLoginDictionary = [[NSMutableDictionary alloc] init];
NSArray *urlComponents = [urlString componentsSeparatedByString:@"&"];
for (NSString *keyValuePair in urlComponents) {
NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="];
NSString *key = [[pairComponents firstObject] stringByRemovingPercentEncoding];
NSString *value = [[pairComponents lastObject] stringByRemovingPercentEncoding];
[waLoginDictionary setObject:value forKey:key];
}
NSLog(@"%@", waLoginDictionary);
我得到這個輸出:
{
"https://www.example.com/product-detail?journey_id" = 123456;
iswa = 1;
}
uj5u.com熱心網友回復:
您所指的答案已過時,作者本人已相應更新。蘋果[URLQueryItem]在URLComponent物件中添加。
試試這個。
迅速
let urlString = "https://www.example.com/product-detail?journey_id=123456&iswa=1"
var dict: [String : String] = [:]
if let urlComponents = URLComponents(string: urlString), let queryItems = urlComponents.queryItems {
for item in queryItems {
dict[item.name] = item.value
}
}
print("dict : \(dict)")
目標 - C
NSString *urlString = @"https://www.example.com/product-detail?journey_id=123456&iswa=1";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSURLComponents *urlComponents = [NSURLComponents componentsWithString:urlString];
NSArray *queryItems = [urlComponents queryItems];
for (NSURLQueryItem *item in queryItems) {
[dict setValue:item.value forKey:item.name];
}
NSLog(@"dict %@", dict);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/466338.html
上一篇:不可能將字串物件轉換為類物件
