初始化 NSDictionary
以下是我的 .h 文件中的内容:
NSDictionary *originalValues;
@property (nonatomic, retain) NSDictionary *originalValues;
这是用于初始化 NSDictionary 的 .m 文件。
@synthesize originalValues;
- (void)viewDidLoad {
// copy original values when view loaded
originalValues = [[NSDictionary alloc] initWithObjectsAndKeys:place.city, @"city", place.cuisine, @"cuisine",
place.latitude, @"latitude", place.longitude, @"longitude", place.name, @"name", place.rating,
@"rating", place.state, @"state", place.street, @"street", place.telephone, @"telephone",
place.timesVisited, @"times visited", place.uppercaseFirstLetterOfName, @"first letter",
place.website, @"website", place.zipcode, @"zipcode", nil];
}
问题是仅添加了前四个对象和键。之后,它们不会被添加到以 place.name、@"name" 开头的字典中。我在整个字典上做了一个 NSLog ,唯一输出的是我提到的前四个值,然后我在 place.name 上做了一个 NSLog ,它输出了一个值,所以我知道也应该为这个键/值输出一些东西一对。我在这里缺少什么吗?我很好奇为什么所有值最初都没有添加到 NSDictionary 中?
The following is in my .h file:
NSDictionary *originalValues;
@property (nonatomic, retain) NSDictionary *originalValues;
This is the .m file to init the NSDictionary.
@synthesize originalValues;
- (void)viewDidLoad {
// copy original values when view loaded
originalValues = [[NSDictionary alloc] initWithObjectsAndKeys:place.city, @"city", place.cuisine, @"cuisine",
place.latitude, @"latitude", place.longitude, @"longitude", place.name, @"name", place.rating,
@"rating", place.state, @"state", place.street, @"street", place.telephone, @"telephone",
place.timesVisited, @"times visited", place.uppercaseFirstLetterOfName, @"first letter",
place.website, @"website", place.zipcode, @"zipcode", nil];
}
The problem is only the first four objects and keys are getting added. After that, they are not being added to the dictionary starting with place.name, @"name". I did a NSLog on the entire dictionary and the only things outputted were the first four values like I mentioned so then II did an NSLog on place.name and it is outputting a value so I know something should also be outputted for this key/value pair. Is there something I am missing here? I'm curious why all of the values are not being initially added to the NSDictionary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果其中一个对象为
nil
,如果您使用新的文字语法来初始化NSDictionary
(如下),则可以更快地捕获该对象。这种语法不仅更短,而且更健壮:如果您的对象之一为nil
,您实际上会收到运行时错误,而不是默默地继续使用不完整的数据执行。If one of the objects is
nil
, you can catch that much faster if you use the new literal syntax for initializing anNSDictionary
(below). This syntax is not only shorter, but also more robust: you'll actually get a runtime error if one of your objects isnil
, instead of silently continuing execution with the incomplete data.它们没有被添加的原因是因为一个对象为零,这标志着字典的结尾。您需要确保每个对象不是
nil
,如果是,您可以使用[NSNull null]
代替它。还可以使用 self.originalValues = ... 来进行正确的内存管理。只需确保使用字典的内容检查/可以处理 NSNull 值即可。使用
gnu 三元扩展
的示例:The reason why they are not being added is because an object is nil which marks the end of the dictionary. You need to be sure that each object is not
nil
and if it is you can use[NSNull null]
in place of it. Also useself.originalValues = ...
for proper memory management. Just make sure what ever uses the dictionary checks/can handleNSNull
values.Example using
gnu ternary extension
:为了避免在任何字典中出现意外的 nil,最好的解决方案是使用文字和三元运算符将两种技术结合起来,例如
To avoid accidental nil in any dictionary the best solution would be if you combine the two technics using the literal and the ternary operator like