0

I have an Array {-1,0,1,2,3,4...} I am trying to find whether an element exist in these number or not, code is not working

NSInteger ind = [favArray indexOfObject:[NSNumber numberWithInt:3]];

in ind i am always getting 2147483647

I am filling my array like this

//Loading favArray from favs.plist
    NSString* favPlistPath = [[NSBundle mainBundle] pathForResource:@"favs" ofType:@"plist"];
    NSMutableDictionary* favPlistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:favPlistPath];

    NSString *favString = [favPlistDict objectForKey:@"list"];
    NSArray *favList = [favString componentsSeparatedByString:@","];
    //int n = [[favList objectAtIndex:0] intValue];

    favArray = [[NSMutableArray alloc] initWithCapacity:100];
    if([favList count]>1)
    {
        for(int i=1; i<[favList count]; i++)
        {
            NSNumber *f = [favList objectAtIndex:i];
            [favArray insertObject:f atIndex:(i-1)];
        }
    }
2
  • can you show how you created the array? I suspect that it is not filled with NSNumber* but rather primitives. I believe NSNumber numberWithInt returns a pointer to a NSNumber object. Commented Jul 8, 2010 at 12:15
  • 2
    @SB: you can't put primitives in an NSMutableArray. Commented Jul 8, 2010 at 12:51

1 Answer 1

5

That's the value of NSNotFound, which means that favArray contains no object that isEqual: to [NSNumber numberWithInt:3]. Check your array.

After second edit:

Your favList array is filled with NSString objects. You should convert the string objects to NSNumber objects before inserting them in favArray:

NSNumber *f = [NSNumber numberWithInt:[[favList objectAtIndex:i] intValue]];
Sign up to request clarification or add additional context in comments.

Comments

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.