【问题标题】:Why am I getting a "String is not NSObject" error in Alamofire?为什么我在 Alamofire 中收到“字符串不是 NSObject”错误?
【发布时间】:2015-04-05 17:39:14
【问题描述】:
let parameters = [
                "alert": model.alert, //Bool
                "distance": model.distance, //Int
                "minAge": model.minAge, //Int
                "maxAge": model.maxAge, //Int
                "access_token": access_token //String
            ]
            Alamofire.request(.POST, Constants.Domain + "/accounts/discovery-settings", parameters: parameters).responseJSON{
                (req, res, json, error) in
            }

我之前做了一个非常相似的请求,它成功了。根据 Alamofire 的说法,我可以将参数设置为任何值,对吧?

编辑:

如果我删除这些行,那么它编译得很好。 Bool 和 Int 类型似乎是问题所在。

            "alert": model.alert, //Bool
            "distance": model.distance, //Int
            "minAge": model.minAge, //Int
            "maxAge": model.maxAge, //Int

型号是:

class DiscoverySettings {
    var alert: Bool
    var distance: Int
    var minAge: Int
    var maxAge: Int
    private init(){
        alert = true
        distance = 10
        minAge = 18
        maxAge = 25
    }
}

https://github.com/Alamofire/Alamofire#post-request-with-url-encoded-parameters

【问题讨论】:

  • 你能说得更具体点吗?这是编译时错误还是运行时错误?它发生在哪条线上?是否突出显示特定字符​​串?如果是运行时错误,能否显示完整的堆栈跟踪?
  • (我没有发现您发布的代码有任何问题。)
  • 你可以尝试写access_token as NSString,因为NSString是NSObject的子类。
  • 这是一个编译错误。 Access_token 不是问题。我对另一个请求也这样做。
  • 看来是这些问题: "alert": model.alert, //Bool "distance": model.distance, //Int "minAge": model.minAge, //Int "maxAge ": model.maxAge, //Int .如果我删除它们,那么它会编译。

标签: ios string swift alamofire


【解决方案1】:

很难说,因为您实际上并没有发布错误,但我猜您在字典中遇到了桥接问题。试试以下方法:

let parameters: [String: AnyObject] = [
    "alert": model.alert, //Bool
    "distance": model.distance, //Int
    "minAge": model.minAge, //Int
    "maxAge": model.maxAge, //Int
    "access_token": access_token //String
]

let request = Alamofire.request(.POST, Constants.Domain + "/accounts/discovery-settings", parameters: parameters)
request.responseJSON { request, response, json, error in
    println(request)
    println(response)
    println(json)
    println(error)
}

希望这有助于阐明一些问题。如果没有,那么请发布您的实际错误,我很乐意修改我的答案。

【讨论】:

    猜你喜欢
    • 2014-10-14
    • 2011-11-02
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多