-1

我有一个 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 随便”

“18这个”

等等

4

1 回答 1

2

我猜这与您之前的问题有关Sort NSArray's by an int contains 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
// )

)

于 2009-12-09T15:03:10.057 回答