-
-
Save vukcevich/91760cdcf1fdac2d21aa3af5a89a6881 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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