Declared in NSString+RestKit.h
NSString+RestKit.m

Overview

A library of helpful additions to the NSString class to simplify common tasks within RestKit

Tasks

  • – appendQueryParams:

    Returns a resource path from a dictionary of query parameters URL encoded and appended This is a convenience method for constructing a new resource path that includes a query. For example, when given a resourcePath of /contacts and a dictionary of parameters containing foo=bar and color=red, will return /contacts?foo=bar&color=red

  • – interpolateWithObject:

    Convenience method for generating a path against the properties of an object. Takes a string with property names prefixed with a colon and interpolates the values of the properties specified and returns the generated path.

  • – queryParameters

    Returns a dictionary of parameter keys and values using UTF-8 encoding given a URL-style query string on the receiving object. For example, when given the string /contacts?foo=bar&color=red, this will return a dictionary of parameters containing foo=bar and color=red, excluding the path “/contacts?”

  • – queryParametersUsingEncoding:

    Returns a dictionary of parameter keys and values given a URL-style query string on the receiving object. For example, when given the string /contacts?foo=bar&color=red, this will return a dictionary of parameters containing foo=bar and color=red, excludes the path “/contacts?”

  • – queryParametersUsingArrays:encoding:

    Returns a dictionary of parameter keys and values arrays (if requested) given a URL-style query string on the receiving object. For example, when given the string /contacts?foo=bar&color=red, this will return a dictionary of parameters containing foo=[bar] and color=[red], excludes the path “/contacts?”

  • – stringByAddingURLEncoding

    Returns a URL encoded representation of self.

  • – stringByReplacingURLEncoding

    Returns a representation of self with percent URL encoded characters replaced with their literal values.

  • – MIMETypeForPathExtension

    Interprets the receiver as a path and returns the MIME Type for the path extension using Core Services.

  • – isIPAddress

    Returns YES if the receiver contains a valid IP address

Instance Methods

MIMETypeForPathExtension

Interprets the receiver as a path and returns the MIME Type for the path extension using Core Services.

- (NSString *)MIMETypeForPathExtension

Return Value

The expected MIME Type of the resource identified by the path or nil if unknown

Discussion

For example, given a string with the path /Users/blake/Documents/monkey.json we would get @“application/json” as the MIME Type.

Declared In

NSString+RestKit.h

appendQueryParams:

Returns a resource path from a dictionary of query parameters URL encoded and appended This is a convenience method for constructing a new resource path that includes a query. For example, when given a resourcePath of /contacts and a dictionary of parameters containing foo=bar and color=red, will return /contacts?foo=bar&color=red

- (NSString *)appendQueryParams:(NSDictionary *)queryParams

Parameters

queryParams

A dictionary of query parameters to be URL encoded and appended to the resource path

Return Value

A new resource path with the query parameters appended

Discussion

NOTE – Assumes that the resource path does not already contain any query parameters.

Declared In

NSString+RestKit.h

interpolateWithObject:

Convenience method for generating a path against the properties of an object. Takes a string with property names prefixed with a colon and interpolates the values of the properties specified and returns the generated path.

- (NSString *)interpolateWithObject:(id)object

Parameters

object

The object to interpolate the properties against

Discussion

For example, given an ‘article’ object with an ‘articleID’ property of 12345 [@“articles/:articleID” interpolateWithObject:article] would generate @“articles/12345” This functionality is the basis for resource path generation in the Router.

See Also

Declared In

NSString+RestKit.h

isIPAddress

Returns YES if the receiver contains a valid IP address

- (BOOL)isIPAddress

Discussion

For example, @“127.0.0.1” and @“10.0.1.35” would return YES while @“restkit.org” would return NO

Declared In

NSString+RestKit.h

queryParameters

Returns a dictionary of parameter keys and values using UTF-8 encoding given a URL-style query string on the receiving object. For example, when given the string /contacts?foo=bar&color=red, this will return a dictionary of parameters containing foo=bar and color=red, excluding the path “/contacts?”

- (NSDictionary *)queryParameters

Parameters

receiver

A string in the form of @“/object/?sortBy=name”, or @“/object/?sortBy=name&color=red”

Return Value

A new dictionary of query parameters, with keys like ‘sortBy’ and values like ‘name’.

Declared In

NSString+RestKit.h

queryParametersUsingArrays:encoding:

Returns a dictionary of parameter keys and values arrays (if requested) given a URL-style query string on the receiving object. For example, when given the string /contacts?foo=bar&color=red, this will return a dictionary of parameters containing foo=[bar] and color=[red], excludes the path “/contacts?”

- (NSDictionary *)queryParametersUsingArrays:(BOOL)shouldUseArrays encoding:(NSStringEncoding)encoding

Parameters

shouldUseArrays

If NO, it yields the same results as queryParametersUsingEncoding:, otherwise it creates value arrays instead of value strings.

encoding

The encoding for to use while parsing the query string.

receiver

A string in the form of @“/object?sortBy=name”, or @“/object?sortBy=name&color=red”

Return Value

A new dictionary of query parameters, with keys like ‘sortBy’ and value arrays (if requested) like [‘name’].

Discussion

This method originally appeared as queryContentsUsingEncoding: in the Three20 project: https://github.com/facebook/three20/blob/master/src/Three20Core/Sources/NSStringAdditions.m

Declared In

NSString+RestKit.h

queryParametersUsingEncoding:

Returns a dictionary of parameter keys and values given a URL-style query string on the receiving object. For example, when given the string /contacts?foo=bar&color=red, this will return a dictionary of parameters containing foo=bar and color=red, excludes the path “/contacts?”

- (NSDictionary *)queryParametersUsingEncoding:(NSStringEncoding)encoding

Parameters

receiver

A string in the form of @“/object/?sortBy=name”, or @“/object/?sortBy=name&color=red”

encoding

The encoding for to use while parsing the query string.

Return Value

A new dictionary of query parameters, with keys like ‘sortBy’ and values like ‘name’.

Discussion

This method originally appeared as queryContentsUsingEncoding: in the Three20 project: https://github.com/facebook/three20/blob/master/src/Three20Core/Sources/NSStringAdditions.m

Declared In

NSString+RestKit.h

stringByAddingURLEncoding

Returns a URL encoded representation of self.

- (NSString *)stringByAddingURLEncoding

Declared In

NSString+RestKit.h

stringByReplacingURLEncoding

Returns a representation of self with percent URL encoded characters replaced with their literal values.

- (NSString *)stringByReplacingURLEncoding

Declared In

NSString+RestKit.h