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 Interacting with Data From the Web Constructing a URL

Amit Kalra
Amit Kalra
3,078 Points

Absolute URL

This challenge is telling me to create an absolute URL, which I think I'm doing, I checked in Xcode, it seems to work fine. Why is the challenge marking this as wrong?

urls.swift
import Foundation

var treehouseCourseID = 25

// Add your code below

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

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

You are using the wrong address in a constant of courseURL which is relative to baseURL. It's just a little error of a parenthesis appears in the courseURL. Check this out.

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

Hopefully it can help you out.

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Amit,

I made EXACTLY this mistake on a PHP project I've been working on, so I feel your pain.

Your base URL ends with a forward slash, and the string you're adding to it starts with a forward slash, so you end up with:

https://api.teamtreehouse.com//course/...

The double slash is the problem. Just drop it from your courseURL constant.

urls.swift
import Foundation

var treehouseCourseID = 25

// Add your code below

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

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

Cheers,

-Greg

Amit Kalra
Amit Kalra
3,078 Points

Haha, thank you! Turns out I had that written in Xcode but couldn't notice this difference in the challenge.

You are using the wrong address in a constant of courseURL which is relative to baseURL. It's just a little error of a parenthesis appears in the courseURL. Check this out.

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

Hopefully it can help you out.