Warrior
Warrior

Reputation: 39374

How to get a particular value from a Url in iOS - Swift 3

I have a url that is reg code attached to it. I just want to retrieve the reg code 7954741093-41547468-3 from the below url

https://mycode.funny.net/mycode/Registrations/7954741093-41547468-3?api-version=2011-04

Upvotes: 1

Views: 546

Answers (2)

André Slotta
André Slotta

Reputation: 14030

i think using URLComponents is a little more robust:

let url = "https://mycode.funny.net/mycode/Registrations/7954741093-41547468-3?api-version=2011-04"
let components = URLComponents(string: url)
components?.path.components(separatedBy: "/").last

Upvotes: 0

l-l
l-l

Reputation: 3854

You can use this:

let regCode:String = (url.components(separatedBy: "/").last?.components(separatedBy: "?").first)!

Where url is

let url:String = "https://mycode.funny.net/mycode/Registrations/7954741093-41547468-3?api-version=2011-04"

Upvotes: 1

Related Questions