Permalink
Browse files

Added +[NSURL URLWithFormat:]

  • Loading branch information...
soffes committed Sep 16, 2011
1 parent 5f403d5 commit c0fc5c389117defaa2a237224eb8e0349d0f08ff
Showing with 42 additions and 0 deletions.
  1. +20 −0 SSToolkit/NSURL+SSToolkitAdditions.h
  2. +13 −0 SSToolkit/NSURL+SSToolkitAdditions.m
  3. +9 −0 Tests/URLCategoryTest.m
@@ -11,6 +11,26 @@
*/
@interface NSURL (SSToolkitAdditions)
+/**
+ Creates and returns an NSURL object initialized with a provided a format string as a template into which the remaining
+ argument values are substituted.
+
+ The string with which to initialize the NSURL object must be a URL that conforms to RFC 2396. This method parses
+ URLString according to RFCs 1738 and 1808. This method expects URLString to contain any necessary percent escape codes,
+ which are `:`, `/`, `%`, `#`, `;`, and `@`. Note that `%` escapes are translated via UTF-8.
+
+ @param format A format string. See "[Formatting String Objects](http://developer.apple.com/library/IOS/#documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html)"
+ for examples of how to use this method, and "[String Format Specifiers](http://developer.apple.com/library/IOS/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1)"
+ for a list of format specifiers. This value must not be `nil`.
+
+ @param ... A comma-separated list of arguments to substitute into format.
+
+ @return An NSURL object initialized with URLString. If the string was malformed, returns nil.
+
+ @exception NSInvalidArgumentException Thrown if `format` is `nil`.
+ */
++ (id)URLWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
+
/**
Returns a new dictionary that contains a dictionary for the receivers query string.
@@ -11,6 +11,19 @@
@implementation NSURL (SSToolkitAdditions)
++ (id)URLWithFormat:(NSString *)format, ... {
+ va_list arguments;
+ va_start(arguments, format);
+ NSString *string = [[NSString alloc] initWithFormat:format arguments:arguments];
+ va_end(arguments);
+
+ NSURL *url = [NSURL URLWithString:string];
+ [string release];
+
+ return url;
+}
+
+
- (NSDictionary *)queryDictionary {
return [NSDictionary dictionaryWithFormEncodedString:self.query];
}
View
@@ -13,6 +13,15 @@ @interface URLCategoryTest : GHTestCase
@implementation URLCategoryTest
+- (void)testURLWithFormat {
+ NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"http://sstoolk.it/documentation/Categories/%@(SSToolkitAdditions).html", @"NSURL"]];
+ NSURL *url2 = [NSURL URLWithFormat:@"http://sstoolk.it/documentation/Categories/%@(SSToolkitAdditions).html", @"NSURL"];
+
+ GHAssertNotNil(url2, nil);
+ GHAssertEqualObjects(url1, url2, nil);
+}
+
+
- (void)testQueryDictionary {
NSURL *url = [[NSURL alloc] initWithString:@"http://sstoolk.it/test?foo=bar&awesome=true"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:

0 comments on commit c0fc5c3

Please sign in to comment.