简体   繁体   English

针对多个keyPath执行valueForKeyPaths

[英]Performing valueForKeyPaths against multiple keyPaths

Let's say I have an object with some number of properties and I load up 1000s of these objects into an array. 假设我有一个具有一定数量属性的对象,并将1000个这些对象加载到数组中。 Next, I perform a series of valueForKeyPaths against these properties: 接下来,我针对这些属性执行一系列valueForKeyPaths

result.property1 = [array valueForKeyPath:@"@sum.property1"];
result.property2 = [array valueForKeyPath:@"@sum.property2"];
result.property3 = [array valueForKeyPath:@"@sum.property3"];
etc...

Summing these properties individually seems pretty inefficient. 单独汇总这些属性似乎效率很低。 Is there a better way besides fast enumerating over the properties and summing them manually? 除了快速枚举属性并手动求和之外,还有没有更好的方法? ie

for(Foo* foo in array) {
    result.property1 += foo.property1;
    result.property2 += foo.property2;
    result.property3 += foo.property3;
}

KVC requires keys to be strings: KVC要求键必须是字符串:

A key is a string that identifies a specific property of an object. 密钥是标识对象的特定属性的字符串。 Typically, a key corresponds to the name of an accessor method or instance variable in the receiving object. 通常,键对应于接收对象中访问器方法的名称或实例变量。 Keys must use ASCII encoding, begin with a lowercase letter, and may not contain whitespace. 密钥必须使用ASCII编码,以小写字母开头,并且不能包含空格。

So the answer as far as I know is unfortunately you can't do this with valueForKeyPath: you would have to manually do it or enumerate over it. 因此,据我所知,不幸的是,您无法使用valueForKeyPath:做到这一点valueForKeyPath:您将不得不手动执行或枚举它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM