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 Creating a URL

Sergio Cisneros
Sergio Cisneros
13,078 Points

I'm just lost on what to do here.

So the first part of the problem was easy and the second task looked simple too. But I've tried a few different things to solve it and I can't get it. I left my code on the first step because I tried a few different wrong solutions and just don't even know which one of those to post. :P Maybe I'm not getting the question, not sure. I'm just lost here. So any help would be awesome. :)

Here's the task

To access information about a course we can use a URL like: https://api.teamtreehouse.com/course/COURSE_ID. Given the variable courseID, create an absolute URL, named courseURL, to retrieve information about a user's progress on a certain course. It should have a path that's relative to the treehouseBaseURL.

urls.swift
import Foundation

let courseID = 25

let treehouseBaseURL = NSURL(string: "https://api.teamtreehouse.com/")

2 Answers

Chase Marchione
Chase Marchione
155,055 Points
  • Since courseURL is absolute, we're going to make it a constant (using 'let', like you used with task 1.)
  • We need to use interpolation to include the courseID in our courseURL, and to avoid hardcoding the ID. Essentially, we need to make sure that whatever the value of courseID is appears in the courseURL, without us literally typing in 25. To use interpolation, a backslash and parentheses are used, with the constant typed inside the parentheses.
  • We need the courseURL to be relative to the treehouseBaseURL: the 'relativeToURL' identifier is what we should use to specify that as our path.

Example code if you're still stuck:

import Foundation

let courseID = 25
let treehouseBaseURL = NSURL(string: "https://api.teamtreehouse.com/")
let courseURL = NSURL(string: "course/\(courseID)", relativeToURL: treehouseBaseURL)
Sergio Cisneros
Sergio Cisneros
13,078 Points

Great explanation! Thank you.

Les Campbell
Les Campbell
26,071 Points

Couldn't you make it like this?

let courseID = 25 let treehouseBaseURL = NSURL(string: "https://api.teamtreehouse.com/course/") and let course = NSURL(string: courseID, relativeToURL: treehouseBaseURL)

Alfred Angkasa
Alfred Angkasa
2,413 Points

I tried that first and still error. the courseID need to convert to a String, rather than keep as an Int.