0

Trying to get index on an object in NSMutableArray. It is returning some garbage value, not getting why it is not returning index of particular item. Below is code i tried.

NSString *type = [dictRow valueForKey:@"type"];

if([arrSeatSel indexOfObject:type])
{
    NSUInteger ind = [arrSeatSel indexOfObject:type];
    [arrTotRows addObject:[arrSeatSel objectAtIndex:ind]];
}

type contains value "Gold". And arrSeatSel contains

 (
"Gold:0",
"Silver:0",
"Bronze:1"

How to check that. Please guide.

4
  • 1
    Is the garbage value "2147483647" by any chance? Commented Jun 5, 2013 at 11:36
  • Can you show your whole relevant data structure? Commented Jun 5, 2013 at 11:36
  • @0x7fffffff ya same exactly. Commented Jun 5, 2013 at 11:38
  • @iPhoneProgrammatically See Jeffery Thomas' answer and this: developer.apple.com/library/mac/documentation/Cocoa/Reference/… Commented Jun 5, 2013 at 11:41

4 Answers 4

7

The value you are getting is NSNotFound. You are getting NSNotFound because @"Gold" is not equal to @"Gold:0".

You should try the following

NSUInteger index = [arrSeatSel indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
    return [obj hasPrefix:type];
}];

if (index != NSNotFound) {
    [arrTotRows addObject:[arrSeatSel objectAtIndex:index]];
}

UPDATE

-indexOfObjectPassingTest: is a running the following loop. NOTE: /* TEST */ is some code that returns true when the correct index is found.

NSUInteger index = NSNotFound;
for (NSUInteger i = 0; i < [array count]; ++i) {
   if (/* TEST */) {
       index = i;
       break;
   }
}

In my first sample, /* TEST */ is [obj hasPrefix:type]. The final for loop would look like.

NSUInteger index = NSNotFound;
for (NSUInteger i = 0; i < [arrSeatSel count]; ++i) {
   if ([arrSeatSel[i] hasPrefix:type]) {
       index = i;
       break;
   }
}

if (index != NSNotFound) {
    [arrTotRows addObject:[arrSeatSel objectAtIndex:index]];
}

I like -indexOfObjectPassingTest: better.

The [obj hasPrefix:type] part is just a different way to comparing strings. Read the -hasPrefix: doc for more details.

Hope that answers all your questions.

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

2 Comments

that i know but i need to apply this thing only, is it possible.
Tried even this way same result. NSUInteger ind = [arrSeatSel indexOfObject:@"Gold:0"];
2

Sometimes storing data properly can solve lot of hazzle. If I guess correctly

"Gold:0" denotes a circle of type Gold an its count 0.

You can try to reformat that to an array of items.

Such as

[
    {
        "Type": "Gold",
        "Count": 0
    },
    {
        "Type": "Silver",
        "Count": 0
    },
    {
        "Type": "Bronze",
        "Count": 1
    }
]

And then using predicate to find the index

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Type == %@",@"Gold"];
NSUInteger index = [types indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    [predicate evaluateWithObject:obj];
}];

2 Comments

no you judged it wrong my friend it is the selection made of particular type.
@iPhoneProgrammatically I was just suggesting a way. Even if its selection it still holds rite. count denotes how many of those selection is in each circle.
1

You can try doing this.

[arrSeatSel enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {

    // object - will be your "type"
    // idx - will be the index of your type.
}];

hope this helps.

Comments

1

If I'm reading that correctly, you're saying that arrSeatSel contains three NSStrings, @"Gold:0", @"Silver:0", and @"Bronze:1", correct?

And then your NSString* type is basically @"Gold"

The first thing is Gold and Gold:0 are different strings, and that's just for starter.

As you are searching for a string in the array, you should take each string out, and do a string matching, not just a comparison. What I'm saying is this:

NSString* str1 = @"This is a string";
NSString* str2 = @"This is a string";
if ( str1 == str 2 ) NSLog(@"Miracle does happen!")

The conditional would never be true even though both NSStrings contain the same value, they are different object, thus are different pointers pointing to different blocks of memory.

What you should do here is a string matching, I will recommend NSString's hasPrefix: method here, as it seems to fit your need.

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.