ios nsdictionary 低平均高
现在,我正在使用我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题的确切答案取决于 NSDictionary 中对象的类型。一般来说,答案是您可以使用键值编码运算符来完成您想要的操作。这是一个例子:
这可能并不比你自己枚举事物更有效,但它更简洁。
如果您的 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:
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 thesevalueForKeyPath
methods return NSNumbers.没有比枚举字典更好的方法了。这主要是因为
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 beNSString
s, they might beNSNumber
s or they might beCHZLolCat
s. 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.