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
kaitlanlang
3,042 PointsSWift Language: Why am I getting in Xcode "extra argument ' completionHandler' in call" when video example works fine
The following code exactly as vide generates the above error, why? when works fine in video. These annoyances really block the flow of learning.
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL, completionHandler: { (location: NSURL!, response: NSURLResponse, error: NSError!) -> Void in println(response)
kaitlanlang
3,042 PointsThe following code eliminated the error in Xcode and differs from video code, perhaps tutorial needs to catch up with Xcode
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in println(response)
however I'm still unable to retrieve the expected json data to the console
Stone Preston
42,016 Pointscan you post a link to the video? I can probably help you out but would have to see the video first
kaitlanlang
3,042 PointsHi stone here is link
the video version of code is with about 40 seconds left of video. Thanks :)
It appears forecastURL needs to be forecastURL! to work. Doing this has got rid of my Xcode error but no json data. ill go back through my api key etc to make certain accurate but already checked. Cant harm to try again
5 Answers
Stone Preston
42,016 Pointsoh silly me, dont know how I missed this. you have your code in didReceieve memory warning:
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
let baseURL = NSURL(string:"https://api.forecast.io/forecast/\(apiKey)/")
let forecastURL = NSURL ( string: "-42.785829,147.061822", relativeToURL: baseURL)
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in println(response)
})
downloadTask.resume()
}
you need to put it in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let baseURL = NSURL(string:"https://api.forecast.io/forecast/\(apiKey)/")
let forecastURL = NSURL ( string: "-42.785829,147.061822", relativeToURL: baseURL)
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in println(response)
})
downloadTask.resume()
}
Stone Preston
42,016 Pointsyes this video was filmed using Xcode 6.0. Xcode 6.1 introduced some changes to a lot of functions which causes issues like this. The teachers notes generally have a note about it when there is a change, however this one does not. You do need to unwrap your URL optional.
Pasan Premaratne any chance you can add a note in the teachers notes about this issue?
Kaitlan Lang post your full code an ill see if I can find an issue with your forecast codeand why its not working.
kaitlanlang
3,042 Pointsanother video dataObject needs to be an optional approx 15 seconds from end of video
http://teamtreehouse.com/library/build-a-weather-app-with-swift/concurrency/using-our-json-data
Pasan Premaratne
Treehouse TeacherThanks for helping out Stone Preston. I'll add the note for now (planning on refreshing the course entirely to cover Xcode changes)
kaitlanlang
3,042 PointsHi this is for viewController.swift
//
// ViewController.swift
// Stormy
//
// Created by xxxxxxx on 5/12/2014.
// Copyright (c) 2014 xxxxxxx. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
private let apiKey = "fdfb6a92c23a4b5edc405b15bc07032d"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
let baseURL = NSURL(string:"https://api.forecast.io/forecast/\(apiKey)/")
let forecastURL = NSURL ( string: "-42.785829,147.061822", relativeToURL: baseURL)
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in println(response)
})
downloadTask.resume()
}
}
Stone Preston
42,016 Pointstry printing something else in the completion handler so we can be sure its getting called:
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
println("in the completion handler")
println(response)
})
kaitlanlang
3,042 Pointsfor future reader, I have changed api key since posting my exposed code
kaitlanlang
3,042 PointsWith the code you supplied nothing in the console either. Grasping at straws but ill see what a shut down and restart does
kaitlanlang
3,042 PointsYay, fantastic. JSON stuff is coming through :) now have to learn what it all means. Happy! (thats an exclamation happy not an optional happy).
Learnt I must be more careful where I place code. Haven't got head around optional vs none and Xcode changes in the context of this video. I think if i just keep plugging away with repetition it will make sense.
Thankyou very much Stone :)
Stone Preston
42,016 Pointsawesome! glad its working. if you wish to read up a bit on optionals you should check out the section about optionals in the Swift eBook. I find the eBook to be a great resource
Stone Preston
42,016 PointsStone Preston
42,016 Pointscan you post a link to the video