custom class method returning nil Swift IOS

Twizzler

Im new to Swift and IOS programming and im stuck on the following.

I have created an model object in this case named user.

Now i want to use my object and send it to my API adapter(in this case with an weather API instead of link)

Im trying to send it from my view controller and use this way to add my class

    var api: API?

When im pushing my send button im sending my object named user to my api class method called SaveEditModel thats this class+method

public class API{

init (){

}
func SaveEditModel(user:User) -> String{

    println(user.firstname)

    let urlPath: String = "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139"
    var url: NSURL = NSURL(string: urlPath)!
    var request1: NSURLRequest = NSURLRequest(URL: url)
    let queue:NSOperationQueue = NSOperationQueue()

    NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
        var err: NSError
        var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
        println("AsSynchronous\(jsonResult)")


    })

    return "test"

}

}

In this case my println and my return aren't working i send it with this code:

    api?.SaveEditModel(user!)

My log when setting a breakpoint says nil

what am i missing?

Antonio

My understanding is that you have an api variable, which should contain an instance of API, but it's nil instead. Are you initializing it? The fact you are using optional chaining indicates that the variable is an optional, and if it's nil this statement:

api?.SaveEditModel(user!)

does nothing.

So, double check if you are assigning an instance of API to the api variable in your code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related