ios nsdictionary 低平均高

发布于 2025-01-08 07:04:50 字数 142 浏览 1 评论 0原文

现在,我正在使用我的 NSDictionary 并运行所有值的循环来查找最低值、最高值并计算平均值。

由于我是IOS新手,我想问是否有更好的方法来做到这一点。有没有? 谢谢

Right now I'm taking my NSDictionary and running a loop of all values to find the low, high, and calculate average.

Since I'm new to IOS I wanted to ask if there is a better way to do this. Is there? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

浅唱ヾ落雨殇 2025-01-15 07:04:50

这个问题的确切答案取决于 NSDictionary 中对象的类型。一般来说,答案是您可以使用键值编码运算符来完成您想要的操作。这是一个例子:

NSNumber *one = [NSNumber numberWithInt:1];
NSNumber *two = [NSNumber numberWithInt:2];
NSNumber *three = [NSNumber numberWithInt:3];

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:one, @"apple", two, @"orange", three, @"pear", nil];
NSLog(@"The max number is %@", [[dictionary allValues] valueForKeyPath:@"@max.intValue"]);
NSLog(@"The min number is %@", [[dictionary allValues] valueForKeyPath:@"@min.intValue"]);
NSLog(@"The mean is %@", [[dictionary allValues] valueForKeyPath:@"@avg.intValue"]);

这可能并不比你自己枚举事物更有效,但它更简洁。

如果您的 NSDictionary 的内容是具有 NSNumber 作为属性的自定义类的对象,您仍然可以通过将属性名称放在 @avg 之后来使用此方法,例如 @avg。我的财产。请注意,这些 valueForKeyPath 方法返回 NSNumbers。

The exact answer to this question depends on what type of objects are in your NSDictionary. The answer in general is that you can probably use Key-Value Coding operators to do what you want. Here is an example:

NSNumber *one = [NSNumber numberWithInt:1];
NSNumber *two = [NSNumber numberWithInt:2];
NSNumber *three = [NSNumber numberWithInt:3];

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:one, @"apple", two, @"orange", three, @"pear", nil];
NSLog(@"The max number is %@", [[dictionary allValues] valueForKeyPath:@"@max.intValue"]);
NSLog(@"The min number is %@", [[dictionary allValues] valueForKeyPath:@"@min.intValue"]);
NSLog(@"The mean is %@", [[dictionary allValues] valueForKeyPath:@"@avg.intValue"]);

This is probably no more efficient than enumerating things yourself, but it is more concise.

If the contents of your NSDictionary are objects of a custom class that have an NSNumber as a property, you can still use this method by placing the property name after the @avg, e.g. @avg.myProperty. Note that these valueForKeyPath methods return NSNumbers.

原谅我要高飞 2025-01-15 07:04:50

没有比枚举字典更好的方法了。这主要是因为 NSDictionary 包含不透明对象。它们可能是 NSString、NSNumber 或 CHZLolCat。它们可以是任何东西。因此,采用低、高和平均方法是没有意义的,因为并非所有事物都有低、高和平均的概念。

但是,如果您向我们展示了一些代码,我们也许能够提供有关实际枚举方法的具体建议。

There isn't a better way to do it than how you're already doing it by enumerating the dictionary. This is mainly because NSDictionary contains opaque objects. They might be NSStrings, they might be NSNumbers or they might be CHZLolCats. They can be anything. So it wouldn't make sense to have a low, high and average method as not everything has the concept of a low, high and average.

However if you showed us some code we might be able to offer specific advice on the actual method of enumeration.

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