0

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);

    person.firstName = self.firstName.text;
    person.lastName = self.lastName.text;
    person.phoneNumber = self.phoneNumber.text;

    [self.entries addObject:person];

Here's my Previous button:

int length;
length = [self.entries count];

if (length > 0) {
    int index;
    index = [self.entries count] - 1;
    NSLog (@"%d the index is", index);

    NSLog(@"object %@", [self.entries objectAtIndex:index]);
} else {
    NSLog (@"No contacts have been entered. No");
}

//NSLog(@"%d is the length - 1 hopefully", length);
//NSLog (@"index at %d is ", length);

I've tried removing the - 1 here:

index = [self.entries count] - 1;

and changing then on the next line putting index--; but nothing seems to work. it just goes back once.

I understand that length is getting the amount of objects in the index, and then -1 but shouldnt i-- at the end of the count / - 1 keep removing it everytime its pressed??

Any ideas? Cheers

2
  • Did you initialize self.entries? Commented Mar 15, 2014 at 3:46
  • yeah I have initialized it Commented Mar 15, 2014 at 3:47

1 Answer 1

1

You're going to keep hitting the same index with that piece of code - you need to store the state somewhere. Who is going to hold onto what the current index is? With a good designed system the model should really take care of this.

You are best off storing the current index into an ivar and updating that every time the button is pressed.

@interface OKAClass ()
  @property (nonatomic, assign) NSInteger currentIndex;
@end

//... initialise the property with a default value

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

//... when the button is pressed decrement the index (you might want some min / max validation or use modulus to loop over and start from the top again)

NSLog(@"%@", self.entries[self.currentIndex--]);
Sign up to request clarification or add additional context in comments.

6 Comments

awesome thanks very much for your help, would it be best to put the initialization of the currentIndex with the -1 into a new method such as updateGUI or something, or best to leave it in the viewDidLoad?
@Javanoob33 thats upto you, if you need it to happen once per load of the view then stick it in viewDidLoad - you might want to extract this into its own method for namespacing and readability if you have a few initialisers.
okay thanks for your help, just one more question. every time I try and hit the previous button the app keeps crashing and I'm getting the ofllowing error message: any ideas?? ) 2014-03-15 13:55:03.903 ContactList[919:a0b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds [0 .. 0]'
[0 .. 0] tells me there is nothing in the array - whats in there? or supposed to be in there? try initialising the array with some data. you might want to do some checking on the entries, like you could do something like self.entries[self.currentIndex-- % self.entried.count] for an infinite cycle loop or self.entries[MAX(0, self.currentIndex--)] to stop at the first index.
ah theres definitely stuff in the array, because what I do when I add data to the array is print it off, so in my NSLog Output I print whatevers in there; example: 2014-03-15 13:55:28.124 ContactList[931:a0b] james 2014-03-15 13:55:28.126 ContactList[931:a0b] ( " james , james, james" ) I'll try your recommendations now, 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.