ios 初始化 NSDictionary

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9195573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:42:27  来源:igfitidea点击:

Initializing NSDictionary

objective-ciosxcodensdictionary

提问by kschins

The following is in my .h file:

以下是我的 .h 文件:

    NSDictionary *originalValues;
    @property (nonatomic, retain) NSDictionary *originalValues;

This is the .m file to init the NSDictionary.

这是用于初始化 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];
}

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?

问题是只有前四个对象和键被添加。之后,它们不会被添加到以 place.name, @"name" 开头的字典中。我在整个字典上做了一个 NSLog,唯一输出的东西是我提到的前四个值,然后我在 place.name 上做了一个 NSLog 并且它正在输出一个值,所以我知道也应该为这个键/值输出一些东西一对。有什么我在这里想念的吗?我很好奇为什么最初没有将所有值添加到 NSDictionary 中?

回答by Macondo2Seattle

If one of the objects is nil, you can catch that much faster if you use the new literal syntax for initializing an NSDictionary(below). This syntax is not only shorter, but also more robust: you'll actually get a runtime error if one of your objects is nil, instead of silently continuing execution with the incomplete data.

如果其中一个对象是nil,那么如果您使用新的文字语法来初始化一个NSDictionary(如下),您可以更快地捕获它。这种语法不仅更短,而且更健壮:如果您的对象之一是nil,您实际上会收到运行时错误,而不是默默地继续执行不完整的数据。

originalValues = @{ @"city"     : place.city, 
                    @"latitude" : place.latitude,
                    // etc.
                  };

回答by Joe

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 niland if it is you can use [NSNull null]in place of it. Also use self.originalValues = ...for proper memory management. Just make sure what ever uses the dictionary checks/can handle NSNullvalues.

它们没有被添加的原因是因为一个对象是 nil,它标志着字典的结束。您需要确保每个对象都不是nil,如果是,您可以使用它来[NSNull null]代替。也self.originalValues = ...用于适当的内存管理。只需确保使用字典检查/可以处理NSNull值的内容。

Example using gnu ternary extension:

使用示例gnu ternary extension

self.originalValues = [[NSDictionary alloc] initWithObjectsAndKeys:
                         place.city ?: [NSNull null], @"city",
                         place.cuisine ?: [NSNull null], @"cuisine",
                         place.latitude ?: [NSNull null], @"latitude",
                         place.longitude ?: [NSNull null], @"longitude",
                         place.name ?: [NSNull null], @"name",
                         place.rating ?: [NSNull null], @"rating",
                         place.state ?: [NSNull null], @"state",
                         place.street ?: [NSNull null], @"street",
                         place.telephone ?: [NSNull null], @"telephone",
                         place.timesVisited ?: [NSNull null], @"times visited",
                         place.uppercaseFirstLetterOfName ?: [NSNull null], @"first letter", 
                         place.website ?: [NSNull null], @"website",
                         place.zipcode ?: [NSNull null], @"zipcode",
                         nil];

回答by BootMaker

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

为了避免在任何字典中出现意外的 nil,最好的解决方案是,如果您使用文字和三元运算符将这两种技术结合起来,例如

self.originalValues = @{ @"city" : (place.city ?: @"city"), 
                      @"latitude" : (place.latitude  ?: [NSNull null]),
                      // etc.
                       };

Note:

(anyValueOrVariable ?: @"anyOtherValueOrVariable")

is an abbrevation and means same as

(anyValueOrVariable != 0) ? anyValueOrVariable : @"anyOtherValueOrVariable"

笔记:

(anyValueOrVariable ?: @"anyOtherValueOrVariable")

是一个缩写,意思与

(anyValueOrVariable != 0) ? anyValueOrVariable : @"anyOtherValueOrVariable"