尝试设置非属性列表对象

时间:2014-01-28 01:47:57

标签: objective-c nsuserdefaults

我是Xcode和这个网站的新手 - 如果我违反了任何协议,我会道歉。

我正在试图对那些离开的人进行排序。在非日历年假期中修订。所以我没有使用NSUserDefaults

'leave-logic'有效但我在将NSMutableDictionary保存到NSUserDefaults时遇到问题。

错误是

  

尝试设置非属性列表对象{   “2014-01-28 00:00:00 +0000”=“A / L”;   }作为键defthisYearsLeave的NSUserDefaults值

我无法看到它来自哪里,因此所有'NSLogs'。

以下是有问题的代码部分和控制台输出

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];

NSDictionary *returnedData = @{selectedDate :sourceViewController.hiddenDuty.text};
[self.thisYearsLeave addEntriesFromDictionary:returnedData];
    NSArray *debugValue = [self.thisYearsLeave allValues];
       for(id value in debugValue)
       {
          if([value isKindOfClass:[NSString class]]) NSLog(@"value is an NSString: %@", value);
       }
self.thisYears = [[self.thisYearsLeave allKeys]mutableCopy];
       for(id key in self.thisYears)
       {
          if([key isKindOfClass:[NSDate class]]) NSLog(@"key is an NSDate: %@", key);
       }
self.leaveCountThisYear = self.thisYears.count;
NSArray *sortedArray = [self.thisYears sortedArrayUsingComparator:
                 ^(id obj1, id obj2)
                 {
                    return [(NSDate*) obj2 compare: (NSDate*)obj1];
                 }
                 ];
for(int i = 0; i < self.thisYears.count; i++)
{
   self.sortedArrayStrings[i] = [mediumdateFormatter stringFromDate:sortedArray[i]];
   NSLog(@"entry in sortedArrayStrings: %@", self.sortedArrayStrings[i]);     //  √
}        
[defaults setObject: self.sortedArrayStrings forKey:@"defsortedArrayStrings"];    //  √

[defaults setObject: self.thisYearsLeave forKey:@"defthisYearsLeave"];
/* X why is this non property-list?  */

控制台返回:

2014-01-28 00:38:46.995 MyWorkingDays[60106:70b] value is an NSString: A/L
2014-01-28 00:38:46.996 MyWorkingDays[60106:70b] key is an NSDate: 2014-01-28 00:00:00 +0000
2014-01-28 00:38:46.996 MyWorkingDays[60106:70b] entry in sortedArrayStrings: 28 Jan 2014
2014-01-28 00:38:46.997 MyWorkingDays[60106:70b] Attempt to set a non-property-list object {
"2014-01-28 00:00:00 +0000" = "A/L";
} as an NSUserDefaults value for key defthisYearsLeave
2014-01-28 00:38:46.999 MyWorkingDays[60106:70b] *** Terminating app due to uncaught exception            'NSInvalidArgumentException', reason: '*** -[NSUserDefaults setObject:forKey:]: attempt to insert non-property list object {
"2014-01-28 00:00:00 +0000" = "A/L";
} for key defthisYearsLeave'

我确信我错过了一位经验丰富的编码人员,这显得非常明显!

1 个答案:

答案 0 :(得分:1)

NSDate不能作为属性列表字典中的键。您要么必须翻转该字典(即,使@"A/L"成为密钥,将日期作为值),或者找到另一种方法来重新组织数据,以使日期永远不是字典中的密钥。