1

so I'm creating a contact list app and I'm storing everything into an NSMutableArray. I've managed to get add function working perfectly. But I'm struggling with the Previous/Next functions.

I can get it to go back one object in the array when pressing previous, but i Can't get it to go back any further? here's my code: I have two extra classes, PhoneBookEntry which is a subclass of the Person class. These classes contain three strings, Firstname,lastname and studentID

- (IBAction)addPerson:(id)sender {
  PhonebookEntry *person = [[PhonebookEntry alloc] init];
  NSLog(@"%@",self.firstName.text);
  self.currentIndex++;
  person.firstName   = self.firstName.text;
  person.lastName    = self.lastName.text;
  person.phoneNumber = self.phoneNumber.text;
  [self.entries addObject:person];
  NSLog(@"%@", self.entries);
  [self arrayLength ];

I've created a property to hold the current index.

@property (nonatomic, assign) NSInteger currentIndex;

I've created a method which then -1 from the currentINdex;

self.currentIndex = [self.entries count] - 1;

heres my button previous.

- (IBAction)btnPrevious:(id)sender {
  //[self prevObject];
  int length;
  length = [self.entries count];

  if (length > 0) {
    NSLog(@"%@", self.entries[self.currentIndex--]);
    // NSLog(@"object %@", [self.entries objectAtIndex:index]);
  } 
  else {
    NSLog (@"No contacts have been entered. No");
  }

I can get it to go back once, but when I press it a second time Iget the following error.

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'

Theres definitely data in there as I get it to print whats in the array, it also prints when I press previous also. happens no matter how many times I press the add button.

thanks

3 Answers 3

3

Better:

Previous Index:

// in order to go back you need to make sure that you're not already
// on index 0 and that you've items to go back to
if([self.entries count] > 1 && self.currentIndex > 0) {
    self.currentIndex--;
}

Next Index:

// in order to go forward you need to make sure that you're not already
// on the last element
if(self.currentIndex < ([self.entries count] - 1)) {
    self.currentIndex++;
}

Some further explanation on the root cause of your problem:

The main problem is, that you use currentIndex--. This will use the old currentIndex (which is 2) before decreasing the value. If you want to use the decreased value in this statement, you have to apply the decreasing before, i.e. by writing --currentIndex instead.

  • --expression = pre-decrement operator
  • expression-- = post-decrement operator

The pre-decrement operator does decrease the value and return the decreased value while the post-decrement operator will decrease the value by 1 and return the initial value.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thanks for your reply. I've added in your code and I believe I understand, but for some reason it is completely skipping over the if statement? should I be removing the self.currentIndex = [self.entries count] - 1; from the viewDidLoad?? Thanks
If you're referring to the previous index if-statement: this is skipped when you've less than 2 entries and your currentIndex is already 0; Do you have more than 1 entry in your array? And do you increase your currentIndex when adding an entry?
yeah I'll have multiple entries into my array. No I haven't been. should I add an self.currentIndex++; into my add button?
In your question you've the self.currentIndex++ added, therefore I though it's already there. However, If you want to make it rock solid (not only going prev-next, but also cycle from 0 to last and vice versa, check out iPatel's response)
2

i think you are starting from end of list

self.currentIndex = [self.entries count] - 1;

if current index is @ 1 and you have 2 items in array adding an other will give you error try this

NSLog(@"%@", self.entries[self.currentIndex--]);

2 Comments

sorry I made a mistake, I originally had that line I just switched it to ++ to see if it would work. still get the same error with that line. Any other suggestions?
This won't work; self.currentIndex-- will not return the decreased value but the initial one. See my response for more details
1

For Previous

 - (IBAction)btnPrevious:(id)sender
    {
        if(self.currentIndex <= 0)
        {
           self.currentIndex = [self.entries count] - 1;
           NSLog(@"%@", self.entries[self.currentIndex])
        }
        else
        {
          self.currentIndex --;
          NSLog(@"%@", self.entries[self.currentIndex])
         }
    }

For Next

    - (IBAction)btnNext:(id)sender
    {
        if(self.currentIndex >= [self.entries count] - 1)
        {
           self.currentIndex = 0;
           NSLog(@"%@", self.entries[self.currentIndex])
        }
        else
        {
          self.currentIndex ++;
          NSLog(@"%@", self.entries[self.currentIndex])
        }
    }

Hope this will working for you.

3 Comments

You should put the decrementation/incrementation into an else statement. Otherwise you will jump from the first item to the one next to the last and from the last item to the second item.
Hi, I've tried using this but it isnt working. Getting the same error I had at the start.
@Javanoob33 - I also tried with above code after read your comment, It's working well in my demo.. Problem will else where.. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.