Question

J'ai un int et pour une raison quelconque, il ne fonctionne pas après 16 ou plus. Voici mon 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:. Ce n'est pas int, c'est la ligne stringWithoutSpaces ... Je ne peux pas comprendre ce qui le cause

(le NSLog, voir ci-dessus z ++) ressemble à ceci:

"Ici"

"whatever"

"17 whatevere"

"18 cette"

etc.

Était-ce utile?

La solution

Je suppose que cela est lié à votre question précédente Trier par NSArray de int contenue dans le tableau , et que vous essayez de dépouiller le nombre de premier plan et des espaces d'un tableau qui ressemble à celui que vous aviez en cette 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"

Sans connaître l'entrée complète, je suppose que votre code échoue probablement parce que les numéros principaux et la valeur de z obtiennent désynchronisés. Puisque vous ne semblez pas se soucier réellement ce que le nombre est premier et que vous voulez juste Vamoose, je vous recommande une approche différente qui recherche les principaux chiffres et extrait la sous-chaîne à partir de la position où ces chiffres se terminent:

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
// )

)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top