ios - swift 3 调用 AFNetworking 的 post() 时函数参数的一个非常奇怪的问题

标签 ios swift

我是 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,我真的不知道它们之间有什么区别?

最佳答案

尝试转换允许的url中的url字符串

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

关于ios - swift 3 调用 AFNetworking 的 post() 时函数参数的一个非常奇怪的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42593318/

相关文章:

swift - ARKit 检测平面之间的相交

xcode - 通过已知的 NSIndexPath swift 获取单元格

iphone - 可以使用哪些编程语言来开发 iPhone、iPod Touch 和 iPad (iOS) 应用程序?

ios - 滚动表格时 UITableViewCell 重新排序控件消失

ios - cellForItemAtIndexPath : 中不是唯一的单元格

ios - Xcode 在更改时重建`

iphone - 是否有网站展示如何重新创建特定的 iOS UI 范例?

ios - 没有互联网 iOS swift 无法接收本地通知

ios - 是否有类/库/框架可以在 iOS 中以 SWIFT 语言调用 Web 服务?

ios - 在 UICollectionView 中禁用滚动并调整其大小以包裹所有单元格