目标是生成长度为NSString的字符,并将每个字符串分配给一个数组。我陷入了我需要用算法做的事情以获得正确的结果。这是样本。我得到的结果是相同的随机生成的字符串添加到我的数组26次而不是添加26个不同的字符串。
我考虑过声明26个不同的NSStrings并将算法的每个结果分配给每个字符串,但这似乎效率低下。谢谢你的帮助。
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);
答案 0 :(得分:2)
我将如何做到这一点:
#import "NSString+Shuffle.h"
NSString * string = @"expert";
NSUInteger strLength = [string length];
NSString * alphabet = @"abcdefghijklmnopqrstuvwxyz";
NSMutableSet * randomWords = [NSMutableSet set];
while ([randomWords count] < 26) {
NSString * newWord = [alphabet shuffledString];
newWord = [newWord substringToIndex:strLength];
[randomArray addObject:newWord];
}
NSLog(@"Random set count %d, contents: %@", [randomWords count], randomWords);
然后,您需要NSString
上定义shuffledString
的类别。此方法将简单地获取字符串中的字符并随机重新排列它们。 Google可以很容易地找到体面的随机播放算法。
我希望你能够了解它的工作原理。我做的唯一修改是使用NSSet而不是NSArray,以及循环的条件是什么。消除了重复随机单词的(微小)可能性。
编辑:因为我感觉很慷慨,所以这是一个基本的shuffledString
实施:
//NSString+Shuffle.h
@interface NSString (ShuffleAdditions)
- (NSString *) shuffledString;
@end
//NSString+Shuffle.m
#import "NSString+Shuffle.h"
@implementation NSString (ShuffleAdditions)
- (NSString *) shuffledString {
NSMutableString * shuffled = [self mutableCopy];
NSUInteger length = [shuffled length];
for (int i = 0; i < (4*length); ++i) {
NSString * randomChar = [shuffled subStringWithRange:NSMakeRange(arc4random() % (length-1), 1)];
[shuffled appendString:randomChar];
}
return [shuffled autorelease];
}
@end
答案 1 :(得分:1)
每次都应该创建一个新的randomString:
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++) {
NSMutableString *randomString = [NSMutableString stringWithCapacity: strLength];
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];
[randomArray addObject: randomString];
}
NSLog(@"Random Array count %i, contents: %@", [randomArray count], randomArray);
答案 2 :(得分:0)
每次循环时都会向阵列添加相同的对象,并在你去的时候覆盖它。你提到过:
我考虑过声明26个不同的NSStrings并分配每个NSStrings 从算法到每个字符串的结果......
这确实是你需要做的。将randomString
的初始化移动到循环中将解决您的问题(在每次循环迭代中获得新的NSMutableString
,而不是使用单个对象)。将randomString
的定义更改为简单类型定义:
NSMutableString *randomString;
然后在你的外循环中添加以下行:
randomString = [NSMutableString stringWithCapacity:strLength];
您不需要更改任何其余代码。