2015-04-05 68 views
-1
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的說法,我可以將參數設置爲任何值,對吧?爲什麼在Alamofire中出現「String is not NSObject」錯誤?

編輯:

如果我刪除這些行,那麼它編譯就好了。 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

+0

你能更具體嗎?這是編譯時還是運行時錯誤?它發生在哪一行?是否突出顯示了特定字符串如果這是一個運行時錯誤,您能顯示完整的堆棧跟蹤嗎? – 2015-04-05 17:45:52

+0

(我沒有看到你發佈的代碼有什麼問題。) – 2015-04-05 17:46:36

+0

你可以嘗試寫'access_token作爲NSString',因爲NSString是NSObject的一個子類。 – erdekhayser 2015-04-05 17:47:11

回答

0

這很難說,因爲你實際上並沒有發佈錯誤,但我猜你有在一個橋接問題字典。請嘗試以下方法:

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

希望這有助於闡明一些原理。如果沒有,那麼請發佈你的錯誤,我很樂意修改我的答案。

相關問題