我是ios编程的新手。现在我无法解释如下的奇怪情况:
let url: String = isFavorite ? "1.1/favorites/create.json" : "1.1/favorites/destroy.json"
post(url, parameters: params, progress: nil, success: { (task, response) -> Void in
let dictionary = response as! NSDictionary
let tweet = Tweet(dictionary: dictionary)
success(tweet)
}, failure: { (task, error) -> Void in
print("like tweet error: \(error.localizedDescription)")
failure?(error)
})
它总是抛出异常:
like tweet error: Request failed: not found (404)
当我尝试如下(直接传递url值而不是变量)时,它是可以的:
post("1.1/favorites/create.json", parameters: params, ...
因为错误是404,所以也许post()方法无法识别url,我真的不知道它们之间有什么区别?
答案 0 :(得分:0)
尝试转换允许网址中的网址字符串
var url: String = isFavorite ? "1.1/favorites/create.json" : "1.1/favorites/destroy.json"
url = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
post(url, parameters: params, progress: nil, success: { (task, response) -> Void in
let dictionary = response as! NSDictionary
let tweet = Tweet(dictionary: dictionary)
success(tweet)
}, failure: { (task, error) -> Void in
print("like tweet error: \(error.localizedDescription)")
failure?(error)
})
答案 1 :(得分:0)
试试这个。:)
let url: URLStringConvertible = isFavorite ? "1.1/favorites/create.json" : "1.1/favorites/destroy.json"
答案 2 :(得分:0)
您可以在回调方法中编辑断点,以查看会发生什么