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

NSURL string? Pulling Data from the Web

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.

I'm not sure what I'm suppose to pass in for the NSURL string. My thought was to pass in the courseID using string interpalation but it didn't work. Help would be much appreciated.

import Foundation

let courseID = 25

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

7 Answers

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

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

I tried that but it tells me that I need to convert the courseID variable to a string.

Can you just put qutoations around the 25:

let courseID = "25"

No it wants me to pass a value in for the NSURL string.

I just entered this in exactly and it said it was right:

import Foundation

let courseID = 25

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

let courseURL = NSURL(string: "course/(courseID)/", relativeToURL: treehouseBaseURL)

Thanks hui yi yeh! After many tries your solution worked. :)

Try this:

import Foundation

let courseID = 25

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

let courseURL = NSURL(string: "course(courseID)/", relativeToURL: treehouseBaseURL)

You needed to put the "course(courseID)/" in the courseURL because it is what you are adding to the base URL