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 trialDelip Rao
1,736 PointsBizzaro compile error
Ok. So I'm looking at the code you’re writing in this video and thinking both apiKey and baseURL are constants so why not write them next to each other like:
class ViewController: UIViewController {
private let apiKey = "super-secret"
private let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
...
}
That gives a compile error: ViewController.Type does not have a member named 'apiKey'
This is weird. I should be able to use a constant right after declaring it. Java/Scala/YouNameIt lets me do that.
Here's a simple case to illustrate this in a playground:
class Foo {
private let a = 5
private let a2 = a * a
}
¯\_(ツ)_/¯
1 Answer
Chris Shaw
26,676 PointsHi Delip,
Properties with a class/struct are inaccessible to each other until the init
or viewDidLoad
method is called for example, so you will need to follow like Pasan is writing his code in the video where the baseURL
constant lives inside viewDidLoad
and it will work.