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

Having trouble interpreting NSURL Class Reference in the Swift documentation.

I'm trying to clear up my understanding on how to decipher Swift documentation.

I'm looking at the NSURL Class Reference documentation online. Under the section titled "Creating an NSURL Object" I see:

init(string:)

This is what I assume Pasan is using. When I click on this, more information expands showing the declaration. The declaration is as follows:

convenience init?(string URLString: String)

Pasan uses the following for a baseURL:

let baseURL = NSURL(string: "blahblahblah")

How does he get this from the documentation? The declaration on the documentation looks like he should be using URLString: not just string:.

Also, what does convenience and init? mean?

I appreciate any insights.

2 Answers

Taking a stab at this...

A class can have one or many designated initializers. In this case, initWithScheme, initWithString and so on.

However, a class can also have secondary convenience initializers. These are sort of shorthand to the designated initializers. They may even set defaults to some of the required parameters.

In this case, NSURL(string: "ENTER STRING") is a convenience initializer.

From the swift documentation:

"Convenience initializers are secondary, supporting initializers for a class. You can define a convenience initializer to call a designated initializer from the same class as the convenience initializer with some of the designated initializer’s parameters set to default values. You can also define a convenience initializer to create an instance of that class for a specific use case or input value type.

You do not have to provide convenience initializers if your class does not require them. Create convenience initializers whenever a shortcut to a common initialization pattern will save time or make initialization of the class clearer in intent."

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html

So what you're saying is that when in the documentation there is something like:

init(string:)

All that means is we can replace "init" with the class name (in this case "NSURL")?

So whenever there is init(require parameters) we should interpret it as classNameGoesHere(required parameters)?

Yes. For example, lets take NSString.

From the documentation, you will find that the convenience initializer for :initWithString is:

convenience init(string aString: String)

Therefore, we can use:

var myNewString = NSString(string: "My String")

In XCode, when you type the class name followed by open parentheses, you will be able to scroll through all the options for initializing your instance along with a tooltips, which are helpful to get going.

Thanks Rhian!