2016-09-06 74 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];