转换ISO 8601个字符串日期createOrUpdateInDefaultRealmWithObject

问题描述:

我有JSON块从REST API在下面的格式返回:转换ISO 8601个字符串日期createOrUpdateInDefaultRealmWithObject

[ 
    { 
    id: 1, 
    locations: [ 
     { 
     arriveAt: "2015-03-14T16:05:16Z" 
     }, 
     { 
     arriveAt: null 
    ] 
    }, 
    ... 
] 

然后我有一些代码在我的项目是这样的:

let trips = json as [NSDictionary] 
let realm = RLMRealm.defaultRealm() 
realm.beginWriteTransaction() 
for trip in trips { 
    Trip.createOrUpdateInDefaultRealmWithObject(trip) 
} 
realm.commitWriteTransaction() 

在我的Location类中,有一个var dynamic var arriveAt: NSDate?。我还将NSDate扩展为fromISO8601String方法,该方法将NSDate初始化为将ISO 8601字符串转换为日期。

有没有一种方法,当Realm试图创建Location对象时,它会自动运行从JSON到NSDate.fromISO8601String的字符串?

嗨戴夫,没有什么内置到领域,但可以为你做到这一点。你需要使用像Realm-JSON或Mantle等变压器。

+0

我最后手动解析了每个JSON块,然后在最后一起拉动它。 – 2015-03-16 13:24:03

+1

这也适用于:),很高兴听到!我们正在研究一种导航解决方案,以使这更容易 – yoshyosh 2015-03-16 14:56:52

+0

如果您可以在将数据从API处理到Realm时应用过滤器,那将会很不错。 – 2015-03-16 15:21:07