Url Parsing in Objective-C

Here’s the translation of the URL parsing example from Go to Objective-C:

Our program will demonstrate how to parse URLs in Objective-C. URLs provide a uniform way to locate resources.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // We'll parse this example URL, which includes a
        // scheme, authentication info, host, port, path,
        // query params, and query fragment.
        NSString *urlString = @"postgres://user:pass@host.com:5432/path?k=v#f";
        
        // Parse the URL and ensure there are no errors.
        NSURL *url = [NSURL URLWithString:urlString];
        if (!url) {
            NSLog(@"Failed to parse URL");
            return 1;
        }
        
        // Accessing the scheme is straightforward.
        NSLog(@"%@", url.scheme);
        
        // User contains all authentication info
        NSLog(@"%@:%@", url.user, url.password);
        
        // The host contains both the hostname and the port,
        // if present.
        NSLog(@"%@", url.host);
        NSLog(@"%@", url.port);
        
        // Here we extract the path and the fragment after
        // the #.
        NSLog(@"%@", url.path);
        NSLog(@"%@", url.fragment);
        
        // To get query params in a string of k=v format,
        // use query. You can also parse query params
        // into a dictionary.
        NSLog(@"%@", url.query);
        NSDictionary *queryParams = [self dictionaryFromQueryString:url.query];
        NSLog(@"%@", queryParams);
        NSLog(@"%@", queryParams[@"k"]);
    }
    return 0;
}

// Helper method to parse query string into dictionary
+ (NSDictionary *)dictionaryFromQueryString:(NSString *)queryString {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSArray *pairs = [queryString componentsSeparatedByString:@"&"];
    for (NSString *pair in pairs) {
        NSArray *elements = [pair componentsSeparatedByString:@"="];
        if ([elements count] > 1) {
            NSString *key = [elements[0] stringByRemovingPercentEncoding];
            NSString *val = [elements[1] stringByRemovingPercentEncoding];
            dict[key] = val;
        }
    }
    return dict;
}

Running our URL parsing program shows all the different pieces that we extracted:

postgres
user:pass
host.com
5432
/path
f
k=v
{
    k = v;
}
v

In this Objective-C version:

  1. We use NSURL to parse the URL string.
  2. Most of the URL components can be accessed directly as properties of the NSURL object.
  3. For query parameter parsing, we’ve added a helper method dictionaryFromQueryString: to convert the query string into an NSDictionary.
  4. Error handling is done by checking if the NSURL object is nil after initialization.
  5. We use NSLog for output instead of fmt.Println.

Note that Objective-C doesn’t have built-in functions to split host and port, so we rely on the NSURL class to provide these separately.

This example demonstrates how to work with URLs in Objective-C, which is commonly used when developing iOS or macOS applications.