我有一个int,并且出于某种原因它在16个左右后才能正常工作。这是我的代码:
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++;
}
编辑:这不是int,它是stringWithoutSpaces行...我无法弄清楚是什么导致它。
所以它(NSLog,见上面的z ++)看起来像这样:
“在这里”
“无所谓”
“17 whatevere”
“18 this”
等
答案 0 :(得分:2)
我猜这与你之前的问题Sort NSArray’s by an int contained in the array有关,并且你试图从一个看起来像你那个问题的数组中删除前导数字和空格:
"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"
在不知道完整输入的情况下,我猜你的代码可能会失败,因为z
的前导数字和值不同步。由于您似乎并不真正关心前导数字是什么,只是想要对其进行修改,我建议使用不同的方法扫描前导数字并从这些数字结束的位置提取子字符串:
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
// )
)