0

目标是生成一个长度为 NSString 的字符并将每个字符串分配给一个数组。我被困在我需要用我的算法做什么才能得到正确的结果。这是示例。我得到的结果是将相同的随机生成的字符串添加到我的数组中 26 次,而不是添加了 26 个不同的字符串。

我考虑过声明 26 个不同的 NSString 并将算法中的每个结果分配给每个字符串,但这似乎效率低下。谢谢您的帮助。

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);
4

3 回答 3

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。此方法将简单地获取字符串中的字符并随机重新排列它们。用谷歌可以很容易地找到体面的洗牌算法。

我希望您对它的工作原理有基本的了解。我所做的唯一修改是使用 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
于 2009-11-03T23:20:09.097 回答
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);
于 2009-11-03T23:24:40.210 回答
0

您每次通过循环都将相同的对象添加到您的数组中,并随时覆盖它。你提到:

我考虑过声明 26 个不同的 NSString 并将算法的每个结果分配给每个字符串......

这确实正是您需要做的。将初始化randomString移入循环将解决您的问题(NSMutableString在每次循环迭代中获取一个新对象,而不是使用单个对象)。将 的定义更改为randomString简单的类型定义:

NSMutableString *randomString;

然后在你的外循环中,添加这一行:

randomString = [NSMutableString stringWithCapacity:strLength];

您不需要更改任何其余代码。

于 2009-11-03T23:19:30.110 回答