简体   繁体   English

领域格式日期JSON swift

[英]Realm format date JSON swift

I had JSON with a date "date_use" : 我有一个日期为“date_use”的JSON:

 {"expense":
    {"id":1,
    "amount":123.3,
    "date_use":"2015-07-04T00:00:00Z"}
 }

I had an error on date format when I execute this code : 执行此代码时,我在日期格式上出错:

self.realm!.create(Expense.self, value:json["expense"].object, update: true)

Error : 错误:

Terminating app due to uncaught exception 'RLMException', reason: 'Invalid value '2015-07-04T00:00:00Z' for property 'date_use' 因未捕获的异常'RLMException'而终止应用,原因:'无效值'2015-07-04T00:00:00Z'属性'date_use'

My question is : What is the good date format for Realm? 我的问题是:Realm的日期格式是什么?

The problem here is not the particular date format, but that JSON doesn't support a native date type at all. 这里的问题不是特定的日期格式,而是JSON根本不支持本机日期类型。 So you have to serialize dates to a string format. 因此,您必须将日期序列化为字符串格式。 Using RFC 3339 , like you have to deal with, is generally a good choice because it avoids ambiguities, so you can stick with that. 使用RFC 3339 ,就像你必须处理一样,通常是一个很好的选择,因为它避免了歧义,所以你可以坚持下去。

Realm's create method doesn't expect deserialized JSON, but a dictionary representation of your object. Realm的create方法不期望反序列化的JSON,而是对象的字典表示。 This expects that you have done already the preprocessing step of transforming date string representations back to Cocoa's native type NSDate . 这要求您已经完成了将日期字符串表示转换回Cocoa的本机类型NSDate的预处理步骤。 This isn't done automatically because there are date formats, which are ambiguous (unlike yours), which eg do not provide information about the timezone. 这不是自动完成的,因为存在不明确的日期格式(与您的不同),例如,不提供有关时区的信息。

Good news is there are excellent third-party libraries eg Realm-JSON , which make it a whole lot easier to deal with that. 好消息是有很好的第三方库,例如Realm-JSON ,这使得它更容易处理。 It brings built-in support for that. 它为此提供了内置支持。 This would also allow to map the property naming scheme returned by your API, eg date_use to names which conforms to the more broadly used camel-case dateUse . 这也允许将API返回的属性命名方案(例如date_use到符合更广泛使用的驼峰式dateUse

If you don't want to introduce another dependency just for that use case, you can use and configure NSDateFormatter to parse dates conforming to your particular subset of the RFC 3339 standard, assuming they always use UTC as timezone, marked through the Z suffix. 如果您不想仅针对该用例引入另一个依赖项,则可以使用和配置NSDateFormatter来解析符合RFC 3339标准的特定子集的日期,假设它们始终使用UTC作为时区,通过Z后缀标记。

// Setup the RFC 3339 date formatter
let rfc3339DateFormatter = NSDateFormatter()
let enUSPOSIXLocale = NSLocale(localeIdentifier: "en_US_POSIX")
rfc3339DateFormatter.locale = enUSPOSIXLocale
rfc3339DateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
rfc3339DateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)

// Convert the RFC 3339 date time string to an NSDate
var json: AnyObject! = nil
var value = json["expense"] as! [String : AnyObject]
let date: NSDate?
if let dateString = value["date_use"] as? String {
    date = rfc3339DateFormatter.dateFromString(dateString)
} else {
    date = nil
}
value["date_use"] = date

// Create your object
self.realm!.create(Expense.self, value: value, update: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM