Как разобрать NSMutable Array и сложить значения - PullRequest
1 голос
/ 27 февраля 2012

У меня проблемы с попыткой сложения (суммирования) всех значений в массиве NSMutable: ProfileItems содержит данные из основного объекта данных и заполняется правильными данными. У меня просто проблемы с анализом через NSMutableArray и добавлением данных profileItems.songLength.

Заранее спасибо

   ProfileItems *profileItems = [profileItemsNSMArray objectAtIndex:indexPath.row];

    //Renumber the rows
    int numberOfRows = [profileItemsNSMArray count];
    NSLog(@"numberofRows: %d", numberOfRows);

    for (int i = 0; i < numberOfRows; i++) 
    {
        int sumOfSongs = sumOfSongs + [[profileItems.songLength] objectAtIndex:i];

        NSLog(@"length: %@",sumOfSongs);
    }

Ответы [ 3 ]

4 голосов
/ 28 февраля 2012

Попробуйте быстрое перечисление, оно будет работать намного быстрее и требует гораздо меньше кода.

int sumOfSongs = 0;

for (ProfileItems *item in profileItemsNSMArray) {
   sumOfSongs = sumOfSongs + [item.songlength intValue]; // use intValue to force type to int
}
0 голосов
/ 27 февраля 2012

Попробуйте привести объекты в NSMutableArray:

ProfileItems *profileItems = (ProfileItems*)[profileItemsNSMArray objectAtIndex:indexPath.row];

int numberOfRows = [profileItemsNSMArray count];

for (int i = 0; i < numberOfRows; i++) 
{
    int sumOfSongs += [[profileItems.songLength] objectAtIndex:i];

    NSLog(@"length: %@",sumOfSongs);
}
0 голосов
/ 27 февраля 2012

используйте intValue функцию для NSMutableArray объекта и используйте %d для печати целого числа.

ProfileItems *profileItems = [profileItemsNSMArray objectAtIndex:indexPath.row];

    //Renumber the rows
    int numberOfRows = [profileItemsNSMArray count];
    NSLog(@"numberofRows: %d", numberOfRows);

    for (int i = 0; i < numberOfRows; i++) 
    {
        int sumOfSongs = sumOfSongs + [[[profileItems.songLength] objectAtIndex:i]intValue]; // use intValue

        NSLog(@"length: %d",sumOfSongs); //use %d for printing integer value
    }
...