Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type CCC doesnt conform to protocol 'NSObjectProtocol'

Tags:

I don't understand why my code doesn't work. Here it is:

class Test: NSURLSessionDataDelegate {      func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {          if(error == nil) {             print("Hallo")         } else {             print(error?.userInfo)         }     }      func createRequest() {          let dictionary = [             "mailAddress":"[email protected]",             .....         ]          let nsData: NSData?         do {             nsData = try NSJSONSerialization.dataWithJSONObject(dictionary, options: NSJSONWritingOptions(rawValue:0))         } catch _ {             nsData = nil         }          let defaultConfigObject = NSURLSessionConfiguration.defaultSessionConfiguration()         let defaultSession = NSURLSession(configuration: defaultConfigObject, delegate: self, delegateQueue: NSOperationQueue.mainQueue())         let url = NSURL(string: "http:...")!         let urlRequest = NSMutableURLRequest(URL: url)         urlRequest.HTTPMethod = "POST"         urlRequest.HTTPBody = nsData         urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")         let dataTask = defaultSession.dataTaskWithRequest(urlRequest)         dataTask.resume()      } } 

And the error:

Type Test does not conform to protocol 'NSObjectProtocol'.

Any ideas?

like image 641
emoleumassi Avatar asked Jan 06 '16 16:01

emoleumassi


1 Answers

If you follow up the inheritance chain, NSURLSessionDataDelegate inherits NSURLSessionTaskDelegate, which inherits NSURLSessionDelegate, which inherits, NSObjectProtocol. This protocol has various required methods like isEqual(_:) and respondsToSelector(_:) which you class does not implement.

Generally what you would do here is make your class inherit NSObject which conforms to NSObjectProtocol:

class Test: NSObject, NSURLSessionDataDelegate {     ... }
like image 135
Brian Nickel Avatar answered Sep 28 '22 00:09

Brian Nickel