custom class method returning nil Swift IOS


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{


    let urlPath: String = ""
    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


    return "test"



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


My log when setting a breakpoint says nil

what am i missing?


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:


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


Login to comment