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
Kim Sorgalla
2,174 PointsHow To Create Class Instances
As maybe most of us I am still on a beginner level, hence I need your help and would appreciate answers with a bit more details :)
1. Multiple method-calling
I am trying to create an iOS app to play sound with AVFoundation Framework. In a sample project I saw below code. I don't understand this. I looked into the NSBundle class reference and see that mainBundle and URLForResource are both methods of the same class. For me this means it has kind of the same hierarchy. But why can I call both methods at the same time as below?
let filePath = NSBundle.mainBundle().URLForResource(fileName, withExtension: fileExt)
2. Creating Class Instances
Is there 2 ways of creating instances of a class? Sometimes I see className() and sometimes className.xxx. So either it is created without calling any methods or it uses Dot-Notation to access methods. Is that also an instance then? I am confused :(
Thanks a lot for your help!
1 Answer
Jhoan Arango
14,575 PointsHello
This is not an easy answer.. lol
First question:
That's a chain of methods, basically what is happening is that NSBundle.mainBundle will return an instance of NSBundle, and then URLForResourse() will access that instance and returns a file name, if it exist. I made an example that does basically the same thing. You can put this in a playground in your Xcode and play with it so you can see the results.
Don't put too much attention to this example, it's just to show you the process of it.
class Person {
var name: String?
var lastName: String?
var secondName: String?
var secondLastName: String?
// Type method
static func fullName() -> Person{
// Here we are creating an instance of Person.
let fullName = Person()
fullName.name = "John"
fullName.secondName = "Sebastian"
fullName.lastName = "Arango"
fullName.secondLastName = "Pacelli"
return fullName
}
// Instance method
func firstName(name: String?) -> String? {
if name == self.name {
return "There is a person with a name \(name!)"
} else if name == self.secondName {
return "There is a person with a second name \(name!)"
} else {
return nil
}
}
// Instance method
func lastName(lastName: String?) -> String? {
if lastName == self.lastName {
return "There is a person with a last name \(lastName!)"
} else if lastName == self.secondLastName {
return "There is a person with a second last name \(lastName!)"
} else {
return nil
}
}
}
var test = Person.fullName().firstName("Carlos")
var peter = Person.fullName().lastName("Pacelli")
Second question:
You create an instance of a class like this.
// Using the same class as the example above.
var person = Person()
// now you can access
person.name
Notice how I did Person(). There is nothing in the parenthesis since this class has no initializer.
If you have any more questions, please let me know. :)
Good luck
Kim Sorgalla
2,174 PointsKim Sorgalla
2,174 PointsWOW, thanks a lot man. A lot of my confusion is gone now :D Thanks for this very detailed answer + those lovely code snippets!