L'objectif est de générer une longueur de caractères NSString et d'affecter chaque chaîne à un tableau. Je suis coincé sur ce que j'ai besoin de faire avec mon algorithme pour obtenir le bon résultat. Voici l'exemple. Le résultat que je reçois est la même chaîne générée aléatoirement ajoutée à mon tableau 26 fois au lieu de 26 chaînes DIFFÉRENTES ajoutées.Algorithme permettant d'ajouter des NSStrings générés aléatoirement à NSMutableArray
J'ai pensé à déclarer 26 NSStrings différents et à affecter chaque résultat de l'algorithme à chaque chaîne, mais cela semble inefficace. Merci pour l'aide.
NSMutableString *string = @"expert";
NSUInteger strLength = [string length];
NSString *letterToAdd;
NSString *finishedWord;
NSMutableString *randomString = [NSMutableString stringWithCapacity: strLength];
NSMutableArray *randomArray = [[NSMutableArray alloc] init];
NSArray *charArray = [[NSArray alloc] initWithObjects: @"a", @"b", @"c", @"d",
@"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m",
@"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w",
@"x", @"y", @"z", nil];
for (int a = 0; a < 26; a++) {
for (int i = 0; i < strLength; i++) {
letterToAdd = [charArray objectAtIndex: arc4random() % [charArray count]];
if([randomString length] < strLength) {
[randomString insertString: letterToAdd atIndex: i];
}
finishedWord = randomString;
}
[randomArray addObject: finishedWord];
}
NSLog(@"Random Array count %i, contents: %@", [randomArray count], randomArray);
a bien fonctionné, merci! –