2016-01-06 19 views
9

Kodumun neden çalışmadığını anlamıyorum. İşte burada:Tip CCC 'NSObjectProtocol' protokolüne uymuyor

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

    } 
} 

Ve hatası:

Type Test does not conform to protocol 'NSObjectProtocol'.

Herhangi bir fikir? Eğer miras zincirinde yukarı takip edersek

cevap

21

, NSURLSessionDataDelegate devralır NSURLSessionDelegate, NSObjectProtocol devralır hangi NSURLSessionTaskDelegate devralır. Bu protokol, sınıfın uygulanmadığı isEqual(_:) ve respondsToSelector(_:) gibi çeşitli gerekli yöntemlere sahiptir.

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

benim sınıf protokolü FBSDKSharingDelegate uygun yapmaya çalıştı ve aynı hata var:

Genellikle burada ne yapacağını sınıf NSObjectProtocol uygun NSObject miras hale getirilmesidir. Alt sınıfımı UIView süper sınıfı ile yapmaya çalıştığımda, protokole uyuldu, hata gitti.