【问题标题】:Converting NSCalendarDate to xml file and back returns __NSTaggedDate将 NSCalendarDate 转换为 xml 文件并返回 __NSTaggedDate
【发布时间】:2016-09-06 08:13:47
【问题描述】:

在 Objective-C (OS X 10.9) 中,我编写了一个 NSDictionary 以使用 [MyDict writeToFile:SavePath atomically:YES]; 归档。我用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 是什么?

【问题讨论】:

  • 你在哪里找到了名为 NSCalendarDate 的类?它在iOS api中吗?可能您正在尝试提及 NSDate。你真的不需要关心 NSTaggedDate 是什么。它可能是类的一些内部表示。
  • @Sandeep 它被标记为 OS X,NSCalendarDate 继承自 NSDate
  • NSCalendarDate 已弃用。 强烈建议不要使用 NSCalendarDate。对于日历计算,您应该使用 NSCalendar、NSDate 和 NSDateComponents 的合适组合,如 Cocoa 的日期和时间编程主题中的日历中所述

标签: objective-c nsdictionary osx-mavericks


【解决方案1】:

好的,我发现了错误。 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] 将 NSDates(因此 __NSTaggedDate)转换为 NSCalendarDate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2010-12-18
    相关资源
    最近更新 更多