快速求和 NSSet 中所有对象的属性的方法?
我认为有一种方法可以快速要求 NSSet
轮询其成员并返回其每个对象中的 NSInteger
属性的总和,但我很可能是将此与 Mac OS X 方面的事情混淆。 Cococa Touch 中存在这个吗?
我能找到的最接近的东西是objectEnumerator
,我想我可以遍历每个对象并增加我自己的变量。 是否存在更好的方法?
I thought there was a way to quickly ask a NSSet
to poll its members and return a sum of say an NSInteger
property in each of its objects, but I may very well be confusing this with the Mac OS X side of things. Does this exist in Cococa Touch?
The closest thing I can find is objectEnumerator
, whereby I suppose I could rifle through each object and increment my own variable. Does the better way exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试查找符合 KVC 的数组/集派生类 (
theSet
) 的每个成员的给定属性 (theIntegerPropertyToSum
) 的总和,您可以可以执行以下操作:If you're trying to find the sum of a given property (
theIntegerPropertyToSum
) for each member of an array/set-derived class that's KVC-compliant (theSet
), you can do the following:为什么不使用简单的目标 C (2)?
坏处:
如果你喜欢计算行数,那么这看起来会更长。 是否还有其他缺点 - 我想知道对于不具有所需“theIntegerPropertyToSum”属性的对象,KVC 方法会发生什么情况?
优势:
我敢打赌,这样调试和性能测试会更容易。 另外,当其他人在一两年内阅读你的代码时,他们就会知道这里发生了什么 - 无论他们是否见过一行 Objective C,这看起来就像实际发生的事情。
Why not use plain objective C (2)?
Disadvantage:
If you like to count lines, then this looks longer. Are there other disadvantages - I wonder what happens with the KVC method with objects that don't have the required 'theIntegerPropertyToSum' property?
Advantage:
I would bet that this debugs and performance tests easier. Plus when someone else reads your code in a year or two they will know what is going on here - whether they have ever seen a line of objective C or not, this looks like what is actually happening.