Skip to content

Instantly share code, notes, and snippets.

@AdamSwinden
Created June 29, 2012 11:21
Show Gist options
  • Save AdamSwinden/3017415 to your computer and use it in GitHub Desktop.
Save AdamSwinden/3017415 to your computer and use it in GitHub Desktop.
NSURL Query Attributes
//
// NSURL+QueryAttributes.m
//
// Created by Adam Swinden on 29/06/2012.
// Copyright (c) 2012 The OTHER Media. All rights reserved.
//
#import "NSURL+QueryAttributes.h"
@implementation NSURL (QueryAttributes)
- (NSDictionary *)queryAttriutes {
NSArray *parts = [self.query componentsSeparatedByString:@"&"];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:[parts count]];
for (NSString *part in parts) {
NSArray *keyValue = [part componentsSeparatedByString:@"="];
if ([keyValue count] != 2) continue;
NSString *key = [[keyValue objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString *value = [[keyValue objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
[attributes setObject:value forKey:key];
}
return [NSDictionary dictionaryWithDictionary:attributes];
}
@end
//
// NSURL+QueryAttributes.h
//
// Created by Adam Swinden on 29/06/2012.
// Copyright (c) 2012 The OTHER Media. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSURL (QueryAttributes)
- (NSDictionary *)queryAttriutes;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment