【发布时间】:2012-12-10 23:08:51
【问题描述】:
如果我们的 API 只需要对象 5 个属性中的 2 个,并且 iPhone 应用程序不需要它们来实例化对象,那么当在参数 NSDicitionary 中使用该对象时,应用程序将崩溃。有人告诉我 NSDictionary 不会让你分配 nil 值,因为当它达到 nil 时,它认为它已经完成了。 Objective-c 是否有办法将对象的非 nil 属性吐出到 NSDictionary 中?
例子:
[Drunk alloc] init];
drunk.started_drinking = [NSDate date];
drunk.stopped_drinking (we don't set this because he is still a drunk)
drunk.fat = YES;
drunk.dumb = YES;
parameters:@{
@"auth_token" :token,
@"name" : drunk.name, @"date_started" : drunk.started_drinking,
@"date_stopped" : drunk.stopped_drinking,
@"prescribing_doctor" : drunk.fat,
@"pharmacy" : drunk.dumb
}
当它到达stopped_drinking 属性时会崩溃。有关如何处理此问题的任何建议?
【问题讨论】:
-
为什么不使用其他值?也许为 NULL?
-
如何为 NSDate 添加 NULL?
-
只需像其他任何操作一样将 NULL 分配给它。