2016-01-06 11 views
9

Nie rozumiem, dlaczego mój kod nie działa. Oto ona:Typ CCC nie jest zgodny z protokołem "NSObjectProtocol"

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() 

    } 
} 

i błąd:

Type Test does not conform to protocol 'NSObjectProtocol'.

Jakieś pomysły?

Odpowiedz

21

Jeśli zastosujemy się do łańcucha dziedziczenia, NSURLSessionDataDelegate dziedziczy NSURLSessionTaskDelegate, która dziedziczy NSURLSessionDelegate, która dziedziczy, NSObjectProtocol. Ten protokół ma różne wymagane metody, takie jak isEqual(_:) i respondsToSelector(_:), których klasa nie implementuje.

Generalnie, co można zrobić tutaj jest uczynić Twój klasy dziedziczą NSObject zgodnego NSObjectProtocol:

class Test: NSObject, NSURLSessionDataDelegate { 
    ... 
}
0

Próbowałem zrobić moja klasa zgodne z protokołem FBSDKSharingDelegate i dostał ten sam błąd. Kiedy próbowałem uczynić moją podklasę z nadklasą UIView zgodną z protokołem, błąd zniknął.

Powiązane problemy