1

I am new to swift so bear with me. I am using NSURLSession and I need to access a delegate I have defined when the response is complete.

This is my class property

var delegate: APIClientDelegate?

Here is the relevant section

    var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()

    var session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue())

    var task = session.dataTaskWithRequest(request){
        (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
        if error == nil {
            var result = NSString(data: data, encoding: NSASCIIStringEncoding)!
            NSLog("request to %@ result %@", request.URLString, result)
            if let delegate = self.delegate{
                delegate.requestDidSucceed(data)
            } else {
                println("nope")
            }
        }
    }
    task.resume()

This all works fine but I am not able to access my delegate delegate. Am I missing something simple here? I have been googling for a while but the problem is I don't know what I don't know :/

Thank you for any help.

EDIT EXPLANATION: The issue was that in this context delegate was nil. This was because I did not understand that the class that adheres to the protocol must set the delegate to be itself. This is where the link to the intended protocol implementation is made and this is what I did not understand.

So in the class that implements the protocol:

var apiClient: APIClient = APIClient()
apiClient.delegate = self
lampwins
  • 920
  • 1
  • 9
  • 25

1 Answers1

0

Your delegate is not defined.

You declare a delegate var of type Optional<APIClientDelegate> but you never sets its value. If you want it to work, you delegate var needs to point to a class that fullfills the requirements set in the protocol for APIClientDelegate.

The Tom
  • 2,790
  • 6
  • 29
  • 33
  • I'm sorry that snippet is a little old, I have updated it but it is mostly the same. Stepping back I see your point but why then does this work: https://github.com/tutsplus/SwiftFromScratch-ToDo-2/blob/master/ToDo/AddItemViewController.swift – lampwins Jun 25 '15 at 06:49
  • The AddItemViewController instance is created somewhere, most probably, where it is created its delegate property is also set. What I recommend is: create your class instance in the delegate (in the viewDidLoad for example). After you created your instance, set its property to self like : var instance = MyCoolClass() instance.delegate = self – The Tom Jun 25 '15 at 06:53
  • See here : https://github.com/tutsplus/SwiftFromScratch-ToDo-2/blob/master/ToDo/ViewController.swift in the prepareForSegue method – The Tom Jun 25 '15 at 06:55
  • 1
    I see, so the class that adheres to the protocol should set the delegate to itself? This makes a lot of since because I was wondering how the link between the delegate call and the intended implementation was to be made. – lampwins Jun 25 '15 at 07:02
  • And in fact that worked! Thank you for explaining that! – lampwins Jun 25 '15 at 07:02
  • Exactly, the class that adheres to the protocol has to be set as the delegate. – The Tom Jun 25 '15 at 07:04