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