【发布时间】:2018-07-09 19:45:10
【问题描述】:
我正在尝试从 NSManagedObject 构建 NSDictionary。我的印象是你可以这样做: NSMutableDictionary *myDict; [myDict setObject:self.title forKey:@"title"]; }
因为字典不能保存 nil 值,所以我先测试 nil。但是,当我使用中断验证属性具有值时,当我构建字典时它是零。它使用断点显示为 nil,并将控制台记录为 NULL。希望有人确认以下是创建字典的有效方法,如果是,为什么字典会为零?
NSString *title = @"Test";
NSNumber *complete = @1;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *lasttouchedstr =[dateFormatter stringFromDate:[NSDate date]];
NSMutableDictionary *myDict;
if (self.title.length>=1) {
[myDict setObject:self.title forKey:@"title"];
}
if (![self.complete isKindOfClass:[NSNull class]]) {
[myDict setObject:self.complete forKey:@"complete"];
}
if (![self.lasttouched isKindOfClass:[NSNull class]]) {
[myDict setObject:lasttouchedstr forKey:@"lasttouchedstr"];
}
NSLog(@"myDict is:%@",myDict)//Logs as NULL
return myDict;
提前感谢您的任何建议。
【问题讨论】:
标签: ios objective-c nsdictionary