我正在清理一些代码并尝试在我的NSManagedObject类中放置一个serverRequest。完全相同的函数在它最初编写的UIViewController中完美地工作,但是,当我在CoreData对象文件中调用它时,我得到以下错误:
Error Domain=com.alamofire.error.serialization.request Code=-1016 "The `parameters` argument is not valid JSON." UserInfo={NSLocalizedFailureReason=The `parameters` argument is not valid JSON.}
我在原始案例和新案例中打印了我的“参数”,它们完全相同。我成功地访问了我在此CoreData对象类中设置的其他端点,但由于某种原因,这个端点失败了。
有任何想法吗?
["answers": ["each": <_TtGCs23_ContiguousArrayStoragePs9AnyObject__ 0x604000c551b0>(
{
answerString = "";
"assigned_emails" = (
"[email protected]"
);
"checklist_id" = 186;
questionId = 4274;
rating = "";
"report_time" = "2018-09-13 19:40:21 +0000";
},
{
answerString = "";
"assigned_emails" = (
"[email protected]"
);
"checklist_id" = 186;
questionId = 293112;
rating = "";
"report_time" = "2018-09-13 19:40:21 +0000";
selected = (
);
}
)
, "checklist_id": 186], "user": ["authentication_token": "TOKEN", "email": "[email protected]"], "count": 2]
["answers": ["each": <_TtGCs23_ContiguousArrayStoragePs9AnyObject__ 0x600000c57ee0>(
{
answerString = "";
"assigned_emails" = (
"[email protected]"
);
"checklist_id" = 186;
questionId = 4274;
rating = "";
"report_time" = "2018-09-13 19:40:21 +0000";
},
{
answerString = "";
"assigned_emails" = (
"[email protected]"
);
"checklist_id" = 186;
questionId = 293112;
rating = "";
"report_time" = "2018-09-13 19:40:21 +0000";
selected = (
);
}
)
, "checklist_id": 186], "user": ["authentication_token": "TOKEN", "email": "[email protected]"], "count": 2]
这些参数在Successful和Unsuccessful案例中完全相同,只是从不同的文件中调用它们。为什么会从AFNetworking获得序列化错误?
我发现了这个问题。我的参数中有一个日期,必须将其转换为String。
我使用此代码来查找具体问题。
if JSONSerialization.isValidJSONObject(params)
{
if let data = try? JSONSerialization.data(withJSONObject: params, options: [])
{
print("JSON data object is: \(data)")
}
}
else
{
do
{
let data = try JSONSerialization.data(withJSONObject: params, options: [])
print("JSON data object is: \(data)")
}
catch let error as NSError
{
print("no bueno: \(error)")
}
}