Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* Example implementation
*/
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
println("Launched with URL: \(url.absoluteString)")
let userDict = self.urlPathToDictionary(url.absoluteString)
//Do something with the information in userDict
return true
}
/*
func urlPathToDictionary(path:String) -> [String:String]?
Takes a url in the form of: your_prefix://this_item/value1/that_item/value2/some_other_item/value3
And turns it into a dictionary in the form of:
{
this_item: @"value1",
that_item: @"value2",
some_other_item: @"value3"
}
Handles everything properly if there is a trailing slash or not.
Returns `nil` if there aren't the proper combinations of keys and pairs (must be an even number)
NOTE: This example assumes you're using ARC. If not, you'll need to add your own autorelease statements.
*/
func urlPathToDictionary(path:String) -> [String:String]? {
//Get the string everything after the :// of the URL.
let stringNoPrefix = path.componentsSeparatedByString("://").last
//Get all the parts of the url
if var parts = stringNoPrefix?.componentsSeparatedByString("/") {
//Make sure the last object isn't empty
if parts.last == "" {
parts.removeLast()
}
if parts.count % 2 != 0 { //Make sure that the array has an even number
return nil
}
var dict = [String:String]()
var key:String = ""
//Add all our parts to the dictionary
for (index, part) in parts.enumerate() {
if index % 2 != 0 {
key = part
} else {
dict[key] = part
}
}
return dict
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.