views:

183

answers:

1

I have an int and for some reason it isn't working after 16 or so. Here's my code:

NSArray *sortedArray; 
sortedArray = [doesntContainAnother sortedArrayUsingFunction:firstNumSort context:NULL];

int count2 = [sortedArray count];
//NSLog(@"%d", count2);
int z = 0;
while (z < count2) {
 NSString *myString = [sortedArray objectAtIndex:z];
 NSString *intstring = [NSString stringWithFormat:@"%d", z];
 NSString *stringWithoutSpaces; 
 stringWithoutSpaces = [[myString stringByReplacingOccurrencesOfString:intstring
                 withString:@""] mutableCopy];
 [hopefulfinal addObject:stringWithoutSpaces];
 NSLog(@"%@", [hopefulfinal objectAtIndex:z]);
 z++;
}

Edit: It's not the int, it's the stringWithoutSpaces line... I can't figure out what's causing it.

So it (the NSLog, see above the z++) looks like this:

"Here"

"whatever"

"17 whatevere"

"18 this"

etc.

+2  A: 

I'm guessing this is related to your earlier question Sort NSArray’s by an int contained in the array, and that you're trying to strip the leading number and whitespace from an array that looks like the one you had in that question:

"0 Here is an object"
"1 What the heck, here's another!"
"2 Let's put 2 here too!"
"3 Let's put this one right here"
"4 Here's another object"

Without know the full input, I'd guess that your code is likely failing because the leading numbers and the value of z are getting out of sync. Since you don't seem to actually care what the leading number is and just want to vamoose it, I'd recommend a different approach that scans for leading digits and extracts the substring from the position where those digits end:

NSArray *array = [NSArray arrayWithObjects:@"1 One",
                                           @"2 Two",
                                           @"5 Five",
                                           @"17 Seventeen",
                                           nil];

NSMutableArray *results = [NSMutableArray array];
NSScanner *scanner;
NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];

for (NSString *item in array) {
    scanner = [NSScanner scannerWithString:item];
    [scanner scanInteger:NULL]; // throwing away the BOOL return value...
                                // if string does not start with a number,
                                // the scanLocation will be 0, which is good.
    [results addObject:[[item substringFromIndex:[scanner scanLocation]]
                         stringByTrimmingCharactersInSet:whitespace]];
}

NSLog(@"Resulting array is: %@", results);

// Resulting array is: (
//    One,
//    Two,
//    Five,
//    Seventeen
// )

)

Jarret Hardie
thanks, it works perfectly~~~~~
Matt S.