2016-09-06 139 views
1

在Objective-C(OS X 10.9)中,我编写了一个NSDictionary以便与[MyDict writeToFile:SavePath atomically:YES];一起存档。我恢复字典与MyDict = [NSDictionary dictionaryWithContentsOfFile:filePath];将NSCalendarDate转换为xml文件并返回__NSTaggedDate

但是,然后我遇到一个包含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?

+0

您究竟在哪里找到名为NSCalendarDate的类?它在iOS API吗?可能你试图提到NSDate。你真的不需要关心NSTaggedDate是什么。这可能是该班级的一些内部表现。 – Sandeep

+0

@Sandeep它被标记为OS X,'NSCalendarDate'继承自'NSDate'。 – Larme

+0

'NSCalendarDate'已弃用。 *强烈推荐使用NSCalendarDate 。对于日历计算,您应该使用适合的NSCalendar,NSDate和NSDateComponents组合,如日历和时间日历和时间编程主题中为Cocoa * – vadian

回答

0

好的,我发现了错误。 NSDictionary只保存NSDate s,而不是NSCalendarDate s。返回的对象是__NSTaggedDate,它继承自NSDate。我发现这个通过调用:DumpObjcMethods(object_getClass(_StartDate))在对象上和它的超类:

Found 9 methods on '__NSTaggedDate' 
'__NSTaggedDate' has method named 'dealloc' of encoding '[email protected]:8' 
'__NSTaggedDate' has method named 'timeIntervalSinceReferenceDate' of encoding '[email protected]:8' 
'__NSTaggedDate' has method named 'initWithTimeIntervalSinceReferenceDate:' of encoding '@[email protected]:8d16' 
'__NSTaggedDate' has method named 'isInToday' of encoding '[email protected]:8' 
'__NSTaggedDate' has method named 'isInTomorrow' of encoding '[email protected]:8' 
'__NSTaggedDate' has method named 'isInYesterday' of encoding '[email protected]:8' 
'__NSTaggedDate' has method named 'isInSameDayAsDate:' of encoding '[email protected]:[email protected]' 
'__NSTaggedDate' has method named 'isEqual:toUnitGranularity:' of encoding '[email protected]:[email protected]' 
'__NSTaggedDate' has method named 'compare:toUnitGranularity:' of encoding '[email protected]:[email protected]' 

幸运的是,我可以NSDate S(并且因此__NSTaggedDate)转换为与NSCalendarDate _StartDate = [NSCalendarDate dateWithTimeInterval:0 sinceDate:_StartDate];