-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

これは、以前の質問である配列に含まれるintでNSArrayを並べ替えることに関連していると思います。また、その質問にあるような配列から先頭の数字と空白を削除しようとしていると思います。

"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 に答える