所以我很高興地使用JSON和setValuesForKeysWithDictionary將數據鎖入我的系統,直到遇到具有NSDate屬性的對象。問題使用setValuesForKeysWithDictionary和JSON設置NSDate
的JSON包含以下格式的日期:
BackOrderDate: "2011-03-15T00:00:00"
這給了我一個錯誤:
'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "BackOrderDate"; desired type = NSDate; given type = __NSCFString; value = 2011-03-15T00:00:00
我使用JSON.NET與MVC與它進行如下設置:
JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
serializerSettings.Converters.Add(new Newtonsoft.Json.Converters.IsoDateTimeConverter());
string json = JsonConvert.SerializeObject(objects, Formatting.None, serializerSettings);
return Content(json);
有關如何處理此問題的任何想法?我不想失去setValuesForKeysWithDictionary的使用,因爲它很快變得很快。
下面是一個很好的替代解決方案:http://www.cimgf.com/2011/06/02/saving-json-to-core-data/ – Slee 2012-08-08 19:51:14