0

My goal is to create an array of favorites in this class and add them to the array when the user clicks the favorite button. My problem is the for loop doesn't happen, meaning [arrayOfFavs count] = 0. I understand the for the first time, but after I add an object, why wouldn't the count increase and the for loop take place?

-(IBAction)favbutton
{
    bool *isThereAlready = true;
    NSLog(@"start for loop");
    for(int i = 0; i < [arrayOfFavs count]; i++)//problem here
    {
        if ([stateName isEqualToString: [[arrayOfFavs objectAtIndex:i] objectAtIndex:0]])
        {
            isThereAlready = false;
            NSLog(@"sent to else");
        }
        NSLog(@"objects in arrayofStateFavs");
    }

    if(isThereAlready)
    {
        [checkedButton setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];

        NSArray *favArray = [NSArray arrayWithObjects:stateName,phoneName,faxName,addressName, nil];

        [[NSNotificationCenter defaultCenter]
     postNotificationName:@"favState"
     object:favArray];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTableViewNotification"
                                                        object:self];

        [arrayOfFavs addObject:favArray];
    }
    else //unfavorite
    {
        [checkedButton setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];

        NSArray *favDetails = [NSArray arrayWithObjects:stateName,phoneName,faxName,addressName, nil];
        [[NSNotificationCenter defaultCenter]
     postNotificationName:@"unfavState"
     object:favDetails];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTableViewNotification"
                                                        object:self];

        for(int i = 0; i < [arrayOfFavs count]; i++)
        {
            if ([stateName isEqualToString: [[arrayOfFavs objectAtIndex:i] objectAtIndex:0]])
            {
                [arrayOfFavs removeObject:[arrayOfFavs objectAtIndex:i]];
                i--;
            }
        }
   }
}

Edit: This is not the same case as the question marked duplicate: the array is initialized and the for loop only loops once or none at all.

Edit 2: here is my code for initializing the array:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Set the Label text with the selected state

    stateLabel.text = stateName;
    phoneLabel.text = phoneName;
    faxLabel.text = faxName;
    addressLabel.text = addressName;

    plistNum = [NSString stringWithFormat:@"tel:%@",phoneName];

    arrayOfFavs = [[NSMutableArray alloc] init];//for this class

}
  • What output do you get? Have you set a breakpoint to examine the contents of the variables? You say in the edit that it executes 1 time in some cases - again what is the contents of the array in this case? Storing a dictionary in the array rather than an array would probably also make for clearer code. Also, you could use `indexOfObjectPassingTest` rather than iterating the array – Paulw11 Oct 22 '15 at 00:44
  • 1
    Please share the code where you initialize the array. – Aaron Oct 22 '15 at 00:48
  • If your for loop runs only once and never again (which I'm not sure you're 100% sure of) why use a for loop? Seems like the wrong thing to do. Why not a different control statement? – Aaron Oct 22 '15 at 00:52
  • The loop is supposed to run for the number of items in the array. when i first start the app and hit the favorite button, the loop runs 0 times(expected). when i hit it once the loop runs once. but when i hit it a third time, the loop doesn't run at all, telling me the count went back to 0 instead of 2. – Meredith Caveney Oct 22 '15 at 01:02
  • @Paulw11 I set a breakpoint at the if statement, and after adding 3 different items to the array and trying to "unfavorite" one, the breakpoint shows there is only one item in the array, whereas there should be 3. – Meredith Caveney Oct 22 '15 at 01:05
  • @Aaron the loop is supposed to run more than once, but whenever I favorite different objects, the for loop doesn't run. – Meredith Caveney Oct 22 '15 at 01:11
  • So single step through the adding process and work out what is happening. – Paulw11 Oct 22 '15 at 01:18
  • Side note - why is the `isThereAlready` a pointer to `bool` instead of a normal `bool`? You need to fix that. – rmaddy Oct 22 '15 at 01:22
  • @Paulw11 I have found messing around with breakpoints, every time the object changes, the array sets back to 0. Every time the object stays the same, the loop runs like it's supposed to, and the array value becomes 1. Where the code fails to work is when I favorite an object, favorite another object, then try to unfavorite the first. Do mutable arrays reset or something when I switch back and forth? – Meredith Caveney Oct 22 '15 at 01:35
  • It might be helpful if you shared all code, everywhere that might be updating `arrayOfFavs`. – Aaron Oct 22 '15 at 12:58
  • @Aaron This is the only place `arrayOfFavs` is changed. I think I know the problem, although I still don't know how to fix it. It seems every time I leave this Detail View Controller, my variables are reset. I tried using NSUserDefaults to save the data but that didn't work so now I'm just trying to retain the data switching from view controller to view controller. – Meredith Caveney Oct 22 '15 at 18:30
  • 1
    Ah. `arrayOfFavs` probably gets cleaned up because your view controller is no longer on-screen. You'll probably have to persist the state of your favorites somewhere other than the view controller. – Aaron Oct 22 '15 at 18:52

2 Answers2

0

Until you share the code where you're initializing your array, we can't truly assume that you are. Please, double check that you're actually creating an NSArray. Remember, in obj-c you can send a message to a nil object.

You need something like this:

arrayOfFavs = @[];
Aaron
  • 7,055
  • 2
  • 38
  • 53
0

This is likely your problem:

bool *isThereAlready = true;

The compiler warns that this is incorrect code. You are initializing a pointer with an int. Try removing the * and see if that works.

-- more --

Do you realize that the code below does not add four items to the arrayOfFavs?

    NSArray *favArray = [NSArray arrayWithObjects:stateName,phoneName,faxName,addressName, nil];
    [arrayOfFavs addObject:favArray];

Instead it adds only one item and that one item is an array that contains four items. If this is intentional, I strongly suggest you make a class that has four NSStrings in it, instead of how you have it.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72
  • Although this was a problem, I fixed it and the code still doesn't run properly. I think it may have something to do with the array resetting once I leave the DetailViewController – Meredith Caveney Oct 22 '15 at 01:49
  • I think the OP does mean to have two-dimensional array. Check the `if` statement in the first `for` loop. – Aaron Oct 22 '15 at 13:01