Reputation: 1227
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.
Upvotes: 0
Views: 1203
Reputation: 42598
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.
Upvotes: 7
Reputation: 10201
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];
}];
Upvotes: 2
Reputation: 8357
If I'm reading that correctly, you're saying that arrSeatSel
contains three NSString
s, @"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 NSString
s 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.
Upvotes: 1
Reputation: 1519
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.
Upvotes: 1