2014-12-03 33 views
1

我有書面以下請求代碼(使用SwiftHTTP庫)NSURLErrorDomain代碼= -1002儘管逃逸限制字符

func performTagRequest(detail : String){ 
    var request = HTTPTask() 
    var formattedDetail = detail.stringByReplacingOccurrencesOfString("\"", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil) 
    var url = "www.gifbase.com/tag/\(formattedDetail)?format=json" 

    url = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! 
    println(url) 
    request.GET(url, parameters: nil, success: {(response: HTTPResponse) in 
    if response.responseObject != nil { 
     let data = response.responseObject as NSData 
     let str = NSString(data: data, encoding: NSUTF8StringEncoding)! 
     let gifMetaArray = str.componentsSeparatedByString(",") as [String] 
     //println("response: \(gifMetaArray))") //prints the HTML of the page 
    } 
    },failure: {(error: (NSError, HTTPResponse?)) in 
     println("error: \(error)") 
    }) 
} 

返回在瀏覽器(ex for "10thingsihateaboutyou")的有效響應而返回錯誤

error: (Error Domain=NSURLErrorDomain Code=-1002 "The operation couldn’t be completed. 
(NSURLErrorDomain error -1002.)" UserInfo=0x7ff22947b640 
{NSErrorFailingURLStringKey=www.gifbase.com/tag/10thingsihateaboutyou?format=json, 
NSUnderlyingError=0x7ff22940d550 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork 
error -1002.)", NSErrorFailingURLKey=www.gifbase.com/tag/10thingsihateaboutyou?format=json}, nil) 

我發現錯誤-1002是NSURLErrorUnsupportedURL,可能是由於沒有正確轉義字符引起的,但是我的印刷品上面的行應該這樣做,我相信。

任何人都有這方面的經驗?

回答

3

我通過擺弄更多來找到答案。我忘了「http://」,因爲我一直被桌面瀏覽器溺愛...