'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_SwiftValue)' with alamofire swift3

回眸只為那壹抹淺笑 提交于 2019-11-30 15:13:52

问题


I've encountered following error while uploading Swift3 using XCode8.

'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_SwiftValue)'

let param: Parameters = [
    "email":txrNRC.text as AnyObject,
    "password":txtPassword.text as AnyObject
]

Please let me know how to solve that issue. I've already tried with let param: NSDictionary but got same error message.


回答1:


First of all in Swift 3 the Objective-C equivalent of id is Any rather than AnyObject, that avoids also the AnyObject castings.

The error message indicates that an illegal type is used (Parameters), JSON supports only string, number, <null> and array / dictionary.

In your case the dictionary is [String:String], a type annotation is not needed at all

let param = [
    "email" : txrNRC.text,
    "password" : txtPassword.text
]

If txrNRC and txtPassword are optionals you need to unwrap them or use the nil coalescing operator to assign a placeholder if the value is nil



来源:https://stackoverflow.com/questions/40326732/nsinvalidargumentexception-reason-invalid-type-in-json-write-swiftvalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!