将 NSCalendarDate 转换为 xml 文件并返回 returns __NSTaggedDate

Converting NSCalendarDate to xml file and back returns __NSTaggedDate

在 Objective-C (OS X 10.9) 中,我用 [MyDict writeToFile:SavePath atomically:YES]; 写了一个 NSDictionary 来归档。我用 MyDict = [NSDictionary dictionaryWithContentsOfFile:filePath];

恢复字典

但是我后来遇到一个包含NSCalendarDate对象的键不再是NSCalendarDate,而是__NSTaggedDate。这给了我以下错误:

2016-09-06 09:19:39.957 MyProject[720:303] -[__NSTaggedDate yearOfCommonEra]: unrecognized selector sent to instance 0x41bd7801f000000d

代码行:

if([AtTansaction.TransactionDate yearOfCommonEra]    == DataArray[cnt].year)

那么如何重构一个NSCalendarDate呢? NSTaggedDate 是什么?

好的,我发现错误了。 NSDictionary 只保存 NSDates,不保存 NSCalendarDates。返回的对象是一个 __NSTaggedDate,它继承自 NSDate。我通过调用找到了这个: DumpObjcMethods(object_getClass(_StartDate)) 在对象及其超类上:

Found 9 methods on '__NSTaggedDate'
'__NSTaggedDate' has method named 'dealloc' of encoding 'v16@0:8'
'__NSTaggedDate' has method named 'timeIntervalSinceReferenceDate' of encoding 'd16@0:8'
'__NSTaggedDate' has method named 'initWithTimeIntervalSinceReferenceDate:' of encoding '@24@0:8d16'
'__NSTaggedDate' has method named 'isInToday' of encoding 'c16@0:8'
'__NSTaggedDate' has method named 'isInTomorrow' of encoding 'c16@0:8'
'__NSTaggedDate' has method named 'isInYesterday' of encoding 'c16@0:8'
'__NSTaggedDate' has method named 'isInSameDayAsDate:' of encoding 'c24@0:8@16'
'__NSTaggedDate' has method named 'isEqual:toUnitGranularity:' of encoding 'c32@0:8@16Q24'
'__NSTaggedDate' has method named 'compare:toUnitGranularity:' of encoding 'q32@0:8@16Q24'

幸运的是,我可以使用 _StartDate = [NSCalendarDate dateWithTimeInterval:0 sinceDate:_StartDate];