Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Weather App with Swift (Retired) Pulling Data From the Web Networking in iOS

Akilah Jones
Akilah Jones
18,397 Points

What's the advantage of using NSURL?

What are the advantages of using the NSURL class over simply using string interpolation for the entire URL, as shown below:

let apiKey = "dddddfffffffffhhhhhhkkkkkk"
let longitude = "37.8267"
let latitude = "-122.423"

let forecastURL = "https://api.forecast.io/forecast/\(apiKey)/\(longitude),\(latitude)"

i.e. What's the advantage of using NSURL instead of a String?

Thanks

AJ

3 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Akilah,

Let's start with an excerpt.

An NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data.

What does that mean?

Well NSURL isn't just for websites, you can use it to access various types of data sources even on the device itself.

Why do you need to use it?

Because it represents a reliable structure that different parts of the Foundation framework can use with 100% consistency as all the different parts of the URL can be accessed separately as well as you can modify it to include or remove anything whereas ambiguous strings can't behave in that way unless you manually build in that behaviour by extending the String object.

Summary!

NSURL is a widely used object within the Foundation framework, it's seamlessly impossible to use just strings everywhere because other objects are expecting an instance of NSURL where data input is considered to be used, it's also good practise to use this as a generic parser for strings that you consider to be an URL since you can break it down into separated parts and store them away for later use whereas strings again don't offer this functionality.

Hope that helps.

Kai Schuerrer
Kai Schuerrer
10,830 Points

Well the NSURL class has tons of useful functions to make your life easier when working with URLs. In this example the advantage is not really that big, but normally you won't have just one request url. So using relativeToURL is already helpful. Check out the documentation to see the real deal of NSURL ^^

Akilah Jones
Akilah Jones
18,397 Points

Thanks to the both of you!

I jumped the gun a bit with my question: at the time I submitted it, the only thing NSURL had been used for was creating a URL and the benefits were not yet apparent...