2015-06-11 7 views
6

ile çağrlanılamıyor. Şu anda Swift 1.2 kodunun bazı bölümlerini Swift 2.0 ile uyumlu hale getiriyorum. Aslında değişir "sendAsynchronousRequest" yapılır anlamaya olamaz - Şu anda tüm istekleri başarısızSwift 2'de 'sendAsynchronousRequest' argüman listesi

NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in}) 

çağırmak olamaz 'sendAsynchronousRequest' tip bir argüman listesi ile '(NSURLRequest, kuyruk: NSOperationQueue, completionHandler: (NSURLResponse !, NSData !, NSError!) -> Void)'

herhangi Fikir sorunun ne var?

cevap

6

sorunsuz çalışır, sendAsynchronousRequest:queue:completionHandler: imzası:

Swift 2 ve Xcode 7 beta ile
class func sendAsynchronousRequest(request: NSURLRequest, 
    queue: NSOperationQueue!, 
    completionHandler handler: (NSURLResponse!, NSData!, NSError!) -> Void) 

ait Ancak imza sendAsynchronousRequest:queue:completionHandler: değişti ve artık etti: Swift 2 ve Xcode 7 beta dönüm, Sonuç olarak

// Note the non optionals, optionals and implicitly unwrapped optionals differences 
class func sendAsynchronousRequest(request: NSURLRequest, 
    queue: NSOperationQueue, 
    completionHandler handler: (NSURLResponse?, NSData?, NSError?) -> Void) 

, kendi completionHandler pa değiştirmek zorunda kalacak rametre uygulaması ve queue parametresinin isteğe bağlı olmadığından emin olun. NSURLConnection.sendAsynchronousRequest yana

4

Sorun, tamamlama bloğundaki örtülü sarılmamış isteğe bağlı seçeneklerle ilgilidir. Sadece bu isteğe yapmak ve Swift 1.2 ve Xcode 6.3 ile

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) in 
    let string = NSString(data: data!, encoding: NSISOLatin1StringEncoding) 
    print("Response \(string!)") 
} 
2

kullanmalıdırlar IOS 9'da kaldırıldı NSURLSessionpublic func dataTaskWithRequest(request: NSURLRequest, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void) -> NSURLSessionDataTask

0

hızlı 3

let url:URL? = URL(string:location) 
    if url == nil { 
     print("malformed url : \(location)") 
    } 

    NSURLConnection.sendAsynchronousRequest(URLRequest(url:url!), 
     queue: OperationQueue.main) 
    { (response: URLResponse?, data: Data?, error: Error?) -> Void in 

    }