0

如何计算键的所有字典中所有数字的总和,例如“小时”?

找不到任何相关信息。

这是我正在谈论的我的财产清单:

<plist version="1.0">
<array>
    <dict>
        <key>Address1</key>
        <string>17 Stanley Road</string>
        <key>Address2</key>
        <string>Essex</string>
        <key>City</key>
        <string>Southend On Sea</string>
        <key>Email</key>
        <string>lmozdzen@gmail.com</string>
        <key>Lessons</key>
        <array>
            <dict>
                <key>LessonComment</key>
                <string></string>
                <key>LessonDate</key>
                <string>05/06/2013</string>
                <key>LessonHomework</key>
                <string></string>
                <key>LessonHours</key>
                <integer>1</integer>
                <key>LessonNext</key>
                <string></string>
                <key>LessonNumber</key>
                <string>1</string>
                <key>LessonTime</key>
                <string></string>
            </dict>
            <dict>
                <key>LessonComment</key>
                <string>Ryun</string>
                <key>LessonDate</key>
                <string></string>
                <key>LessonHomework</key>
                <string>T</string>
                <key>LessonHours</key>
                <integer>2</integer>
                <key>LessonNext</key>
                <string>Ty</string>
                <key>LessonNumber</key>
                <string>4</string>
                <key>LessonTime</key>
                <string></string>
            </dict>
        </array>

我想要关键“LessonHours”的所有数字的总和

4

1 回答 1

1

使用键值编码

NSNumber *sum = [[yourDict allKeys] valueForKeyPath:@"@sum.theIntegerPropertyToSum"];

或者您可以使用循环使用平面旧样式添加。

NSInteger sum=0;
for(NSString *string in yourDict){
    sum+=[string integerValue];
}
于 2013-06-10T17:33:53.747 回答