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

Trouble converting the course ID to a variable string

I am getting the error "need to convert the course ID to a variable string to use it in the URL. " I don't know another way to do that than what I have done. I changed let to var and put the number in ""

urls.swift
import Foundation

var courseID = "25"

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


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

2 Answers

Stone Preston
Stone Preston
42,016 Points

you need to use string interpolation to interpolate the course ID onto the end of the string.

since the full url used to access a course looks something like "https://api.teamtreehouse.com/course/COURSE_ID", you can see that /course/COURSE_ID is the part of the url thats going to be relative to the base URL, which is "https://api.teamtreehouse.com/".

so interpolate the value of courseID onto the end of the string "course/" and use that as the string parameter.

import Foundation

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

Why do we have to write out string in this instance and why isn't it capitalized. I thought that SWIFT automatically deduced what type of data we were using. And, when required, we should capitalize it. I this because we are out of the apple ecosystem when dealing with websites?

Stone Preston
Stone Preston
42,016 Points
NSURL(string: "course/\(courseID)", relativeToURL: treehouseBaseURL);

the string after NSURL( is a parameter name, not a reference to a data type.

you are calling an initializer for the NSURL class and you must provide parameter names when calling initializer. so you have to provide the parameter name for the 2 parameters. The first parameter in this case happens to be named "string"

It is not capitalized because it is not in reference to the String class, its just the name of a parameter