0

In my app, I am needing to loop through each NSArray, get the NSInteger associated with the key 'people' in the NSArray, and then add them all together. What would be a good starting point for first retrieving each specific NSInteger from each NSArray?

The array in question returns like this in console.

(
    "<Prayers:DDomBXIONY:(null)> {\n    Anonymous = 1;\n    DeviceID = 123;\n    FirstName = Name;\n    LastName = Name;\n    Location = HI;\n    PrayerWarriors = 8;\n    Request = Hi;\n    Title = Hi;\n    UserId = RtXN6QZsgaIiPw4SjFWGtkxXx;\n    dateMade = \"Jan_09_2015\";\n}"
)

Basically just need to retrieve NSInteger from each PrayerWarriors key, and add them all together.

(
    "<Prayers:DDomBXIONY:(null)> {\n    Anonymous = 1;\n    DeviceID = 123;\n    FirstName = Name;\n    LastName = Name;\n    Location = HI;\n    PrayerWarriors = 8;\n    Request = Hi;\n    Title = Hi;\n    UserId = RtXN6QZsgaIiPw4SjFWGtkxXx;\n    dateMade = \"Jan_09_2015\";\n}",
    "<Prayers:yG7GC4bCIH:(null)> {\n    Anonymous = 1;\n    DeviceID = 123;\n    FirstName = Name;\n    LastName = Name;\n    Location = bye;\n    PrayerWarriors = 0;\n    Request = bye;\n    Title = bye;\n    UserId = RtXN6QZsgaIiPw4SjFWGtkxXx;\n    dateMade = \"Jan_09_2015\";\n}"
)
user717452
  • 33
  • 14
  • 73
  • 149
  • NSArray doesn't use keys like 'people' to reference values, it uses an index integer. Not sure what you're asking exactly. – Adama Jan 09 '15 at 23:12
  • It's a little different array as it comes from a PFObject using Parse. I posted the sample of one. @Adama – user717452 Jan 09 '15 at 23:16
  • Ok so I don't know parse object and stuff and so I can't give you an answer, but, maybe a lead, did you check at some keypath methods ? http://nshipster.com/kvc-collection-operators/ maybe something like @unionOfObjects.people – Boris Charpentier Jan 09 '15 at 23:20
  • Post some code snippets. Where you insert the object and where you try to retrieve the object would probably be enough. – Scott Jan 09 '15 at 23:58

2 Answers2

0

So without understanding exactly how your PFObject works, I'm going to assume it's like a dictionary.

Add each of your objects to a single array:

NSMutableArray *arrayHoldingPrayersObjects = [NSMutableArray alloc] init];
arrayHoldingPrayersObjects[0] = prayerObject1;
arrayHoldingPrayersObjects[1] = prayerObject2;
arrayHoldingPrayersObjects[2] = prayerObject3;

etc...

Then create a integer variable outside of a for loop and iterate through your objects, adding the value for PrayerWarrior at each iteration.

int totalPrayerWarriors = 0;
for (int i = 0; i < arrayHoldingPrayersObjects.count; i++)
{
    NSMutableDictionary *prayerObject = arrayHoldingPrayersObjects[i];
    totalPrayerWarriors += [prayerObject objectForKey:@"PrayerWarriors"] intValue];
}

You should end up with a correct total from all arrays. Do some tests to make sure it's accurate for you. Hope this helps.

*EDIT

The error you're getting indicates that it actually is a NSMutableArray, which you can't access using methods like objectForKey, so... there must be a method provided by PFObject that allows you to do that. OR, if PrayerWarriors is reliably the [5]th value (including 0) in the array, then you might be able to access it by index.

replace the lines:

NSMutableDictionary *prayerObject = arrayHoldingPrayersObjects[i];
totalPrayerWarriors += [prayerObject objectForKey:@"PrayerWarriors"] intValue];

with

NSMutableArray *prayerObject = arrayHoldingPrayersObjects[i];
totalPrayerWarriors += prayerObject[5];
Adama
  • 1,101
  • 8
  • 26
  • The NSMutableDictionary is working, I get the dictionary of everything, but it crashes on the next line with error `[__NSArrayM objectForKey:]: unrecognized selector sent to instance` – user717452 Jan 09 '15 at 23:42
  • `'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 1]'` is the new error – user717452 Jan 09 '15 at 23:51
  • Sorry, can't help ya then. You'll need to re-post your question and ask more specifically how to access the content from your PFObject. This is a Parse specific problem. – Adama Jan 09 '15 at 23:56
0

Not sure where the mutable array is coming from. Parse.com will always produce immutable arrays as far as I know. So lets say you've retrieved Prayers with:

PFQuery *query = [PFQuery queryWithClassName:@"Prayers"];
[query findObjectsInBackgroundWithBlock:^(NSArray *prayers, NSError *error) {
    // see how it's an NSArray, not mutable
}];

Now you want the total of the retrieved PrayerWarrior attributes, so...

[query findObjectsInBackgroundWithBlock:^(NSArray *prayers, NSError *error) {
    NSInteger total = 0;
    for (PFObject *p in prayers) {
        NSNumber *warriorCount = p[@"PrayerWarriors"];
        total += [warriorCount intValue];  // notice we cannot add nsnumbers directly (we need intValue)
    }
    NSLog(@"total warriors = %d", total);
}];
danh
  • 62,181
  • 10
  • 95
  • 136