0

In my application I need to build an url like :

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

In order to be able to build the url, I need to access the objects in an NSDictionary, since NSDictionary is an unordered data set, I need to sort the objects alphabetically in order to build the correct url:

NSDictionary

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

I have tried doing this by writing this block of code:

Accessing the objects:

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];

and building the full url like this :

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

This method works fine for now, however I was wondering if this is the correct way of doing this ? is there a better way of implementing this ?

For example as it's mentioned here : Joe's answer ,NSURLQueryItem is used to access objects from dictionaries and build queries from it, however when I used NSURLQueryItem the full url was built with ? and = signs.

Are there any other methods that can be used to just get all of the objects in an NSDictionary ?

2 Answers2

0

When accessing values from an NSDictionary there's no guarantee what type it will be. With full type-checking, a safer and more readable way of creating your URL might be something like:

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
}

This also ensures the URL is generated as you wanted (lowercase "league1" without leading/trailing whitespace) given your input JSON.

  • This answer seems to me correct but only one think you need to correct is instead of checking class .. you just write NSString *section = [redirect[@"section"] string]; This is more sophisticated. – Ashok Londhe Oct 18 '17 at 12:05
0

Try this code.

//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);

It will return ULR same as you want : https://www.thefootballapi/football/League1/player/stats

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
  • you are assigning the values hardcoded. it should be dynamic instead of static – Ashok Londhe Oct 18 '17 at 10:35
  • @AshokLondhe, I write basic code, as per requirement need to change code. – Kuldeep Oct 18 '17 at 10:38
  • Answer should be according to users need – Ashok Londhe Oct 18 '17 at 10:39
  • @AshokLondhe in my case it works, all I needed to do was this : NSArray * are = [[redirect allValues] .........] –  Oct 18 '17 at 10:41
  • @AshokLondhe I think his answer is fine, it can also be useful for anyone that might have this issue, thats in my opinion tbh. –  Oct 18 '17 at 10:42
  • @TahaAmini NSDictionary is non ordered collection set. so if you are fetching the all values from it it should not be in ordered . So i didn't get how you are saying this answer is working for you. Please read the document of NSDictionary. Without having correct concepts don't just copy and paste the answers it doesn't work for future. – Ashok Londhe Oct 18 '17 at 12:01
  • @AshokLondhe, If we are fetching the all values from NSDictionary, It should not be in ordered. But did you see "sortedArrayUsingSelector" Line, in which I sorted NSDictionary All values in Alphabetical Order? – Kuldeep Oct 18 '17 at 12:05
  • @TahaAmini Then you need to correct you question. Please mention that you want to form using for keys Alphabetical Order. That should be in the question. – Ashok Londhe Oct 18 '17 at 12:08
  • @AshokLondhe Thanks, I didn't just copy and paste the answer, I was just saying Ashok's answer was correct although mike's answer is more relevant to my code, I will read up more about NSDictionaries the more knowledge the better and I don't necessarily think "form using for keys alphabetical order" is needed in the question because I was asking generally about better ways of building urls from NSDictionaries –  Oct 18 '17 at 13:38
  • @AshokLondhe however if you think it would be more useful for future readers then I will add it in –  Oct 18 '17 at 13:39
  • 1
    @TahaAmini i have give any answer i was just suggesting better way so that it will helps to everyone. Please add proper requirements into question. If you want keys in alphabetical order that must be mentioned into question. So that everyone is getting your question correctly. – Ashok Londhe Oct 18 '17 at 14:39