初始化 NSDictionary

发布于 2025-01-03 11:05:13 字数 1016 浏览 0 评论 0原文

以下是我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从此见与不见 2025-01-10 11:05:13

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

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

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.

originalValues = @{ @"city"     : place.city, 
                    @"latitude" : place.latitude,
                    // etc.
                  };
乖乖 2025-01-10 11:05:13

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

使用 gnu 三元扩展的示例:

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];

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 use self.originalValues = ... for proper memory management. Just make sure what ever uses the dictionary checks/can handle NSNull values.

Example using 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];
悟红尘 2025-01-10 11:05:13

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

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

注意:

(anyValueOrVariable?:@“anyOtherValueOrVariable”)

是缩写,含义与

相同

(任何值或变量!= 0)?任何值或变量:
@“任何其他值或变量”

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

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文