1

Edited to add code:

I am having an issue with indexOfObject. I am using the user selection from one array to choose and display objects from two other arrays. I first get the index of the object they choose and then use that to get the objectAtIndex from the other two arrays. I know this is very basic stuff, and I have done this before, but i have never experienced an issue like this one...

When the string in the array begins with a letter (a, b, c...) everything works fine. When the string begins with a number, the indexOfObject comes back with a ridiculously long number and crashes the app. The array that the users choose are age groups, so if they choose Premature, Newborn, adult female, or adult male everything works fine. If they choose 1 Year, 2 Year, 4 Year, etc. The app crashes.

This is my code (i changed the names of variables and functions/methods)

This is what is returned from a separate table view

- (void)ageGroupPicked:(NSString *)age {

//Set the text for the ageGroupLabel passed from ageTableView
ageGroupLabel.text = [NSString stringWithFormat:@"%@", age];

//Pass the string to the displayResults method
[self displayResults:age];

}

  • (void)displayResults:(NSString *)display {

    // Set the variable pAge to the array index of the type from the ageGroupArray NSInteger pAge = [self.ageGroupArray indexOfObject:display];

    // Use pAge to set the text of the resultsOneLabel self.resultsOneArray.text = [self.resultsOneArray objectAtIndex:pAge];

    // Use pAge to set the text of the resultsTwoLabel self.resultsTwoLabel.text = [self.resultsTwoArray objectAtIndex:pAge];

}

I am not a full-time programmer, so go easy on me.

3
  • 1
    That ridiculously long number would be NSNotFound, simply meaning the object you are looking for was not found. Can you reproduce the problem on a small code sample? (See sscce.org for a guide.) Commented Nov 8, 2016 at 16:24
  • My point was more that indexOfObject is only working when the object begins with a letter. Why would indexOfObject care if the string begins with a number or letter. I know the objects are in the array because i set up an NSLog to show me they are there. Commented Nov 8, 2016 at 20:38
  • It doesn’t care about letters and numbers. The problem has to be elsewhere, but it’s impossible to tell without you showing us more of your code. Commented Nov 9, 2016 at 6:41

1 Answer 1

1

Check the result index against NSNotFound:

NSInteger index = [self.ageArray indexOfObject:myObject];
if (index == NSNotFound) {
    //the array does not contain myObject
}
Sign up to request clarification or add additional context in comments.

3 Comments

My point was more that indexOfObject is only working when the object begins with a letter. Why would indexOfObject care if the string begins with a number or letter. I know the objects are in the array because i set up an NSLog to show me they are there.
Sounds weired. Please update the question with reproducible example code and/or log of data.
Also tag your target OS (iOS/macOS)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.