从NSDictionary构建URL

时间:2017-10-18 10:02:50

标签: ios objective-c

在我的应用程序中,我需要构建一个URL:

  

https://www.thefootballapi/football/league1/player/stats

为了能够构建url,我需要访问NSDictionary中的对象,因为NSDictionary是一个无序数据集,我需要按字母顺序对对象进行排序以构建正确的URL:

NSDictionary

{
    category = "football";
    league = " League1 " ;
    section = player;
    "sub_category" = "stats";
}

我试过写这段代码来做到这一点:

访问对象:

NSArray *keyyy0= [self.redirect allKeys];
id aaKey0 = [keyyy0 objectAtIndex:0];
id aanObject0 = [self.redirect objectForKey:aaKey0];

NSArray *keys = [self.redirect allKeys];
id aKey = [keys objectAtIndex:1];
id anObject = [self.redirect objectForKey:aKey];

NSArray *keyyy = [self.redirect allKeys];
id aaKey = [keyyy objectAtIndex:2];
id aanObject = [self.redirect objectForKey:aaKey];

并构建完整的网址:

NSString *fullurl = [NSString stringWithFormat:@"%@%@%@%@", newurl,anObject,aanObject,aanObject3 ];

此方法现在运行正常,但我想知道这是否是正确的方法?有没有更好的方法来实现这个?

例如,正如这里提到的那样:Joe's answerNSURLQueryItem用于从字典中访问对象并从中构建查询,但是当我使用NSURLQueryItem时,完整的URL是使用?构建的和=标志。

是否还有其他方法可用于获取NSDictionary中的所有对象?

2 个答案:

答案 0 :(得分:0)

从NSDictionary访问值时,无法保证它的类型。通过完整的类型检查,创建URL的更安全,更易读的方式可能是:

NSDictionary *redirect = @{@"category"      : @"football",
                           @"league"        : @" League1 ",
                           @"section"       : @"player",
                           @"sub_category"  : @"stats"};

id category = redirect[@"category"];
id league = redirect[@"league"];
id section = redirect[@"section"];
id subCategory = redirect[@"sub_category"];
if ([category isKindOfClass:[NSString class]] &&
    [league isKindOfClass:[NSString class]] &&
    [section isKindOfClass:[NSString class]] &&
    [subCategory isKindOfClass:[NSString class]])
{
    NSString *urlString = [NSString stringWithFormat:@"https://www.thefootballapi/%@/%@/%@/%@",
                           [((NSString*)category).lowercaseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]],
                           [((NSString*)league).lowercaseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]],
                           [((NSString*)section).lowercaseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]],
                           [((NSString*)subCategory).lowercaseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
    NSLog(@"%@", urlString); // https://www.thefootballapi/football/league1/player/stats
}

这也可以确保在您输入JSON的情况下生成URL(小写" league1"没有前导/尾随空格)。

答案 1 :(得分:0)

试试这段代码。

//Your Dictionary
    NSMutableDictionary *dict = [NSMutableDictionary new];

        [dict setValue:@"football" forKey:@"category"];
        [dict setValue:@"League1" forKey:@"league"];
        [dict setValue:@"player" forKey:@"section"];
        [dict setValue:@"stats" forKey:@"sub_category"];


// Get desired URL like this
        NSArray *arr = [[dict allValues] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

        NSString *strURL = [NSString stringWithFormat:@"https://www.thefootballapi/%@/%@/%@/%@", [arr objectAtIndex:0], [arr objectAtIndex:1], [arr objectAtIndex:2], [arr objectAtIndex:3]];

        NSLog(@"%@", strURL);

它将根据您的需要返回ULR: https://www.thefootballapi/football/League1/player/stats