Obj-c Shuffle Array没有重复

时间:2016-08-14 02:36:45

标签: objective-c iphone arrays xcode

我为此疯狂。我试图从.txt文档中移植一个数组。但也永远不要重复输出。如果行为0,请执行alert或smth

我目前有这个代码,我无法弄清楚如何计算并从数组中删除呈现的对象。检查了很多帖子并尝试但没有成功..

// declare randomStory as int
int randomStory;

// declare storyString as string
NSString *storyString;

// create variables for text file

// get chronices from text file name
NSString *title = @"chronicles";
// attribute name
NSString *type = @"txt";
// seperation
NSString *separation = @"____________________________________________";
// encoding and variable
NSString *fileText = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:title ofType:type] encoding:NSUTF8StringEncoding error:nil];

// create array with seperated text by \n
NSMutableArray *storyArray = [[NSMutableArray alloc]initWithArray:[fileText componentsSeparatedByString:separation]];

// randomize one text from the text file
randomStory = arc4random()%[storyArray count];

storyString = [storyArray objectAtIndex: randomStory];

// output in storyLabel textView
_storyLabel.text
= storyString;

EDIT:
上帝的缘故,无法粘贴代码。以下是它的外观和SIGABRT错误。 http://pastebin.com/21hvrzwU

1 个答案:

答案 0 :(得分:2)

问题是(如果OP代码全部存在于单个方法中),每次调用该方法都会从文件重建一个新数组。最好以这种方式进行重组:

  1. 将文件读取到它自己的方法“
  2. 构建一个也只运行一次的NSArray shuffler
  3. 使用混洗数组但是你需要它在循环中
  4. - (NSArray *)arrayFromFile {
        NSString *title = @"chronicles";
        NSString *type = @"txt";
        NSString *separation = @"____________________________________________";
        NSString *fileText = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:title ofType:type] encoding:NSUTF8StringEncoding error:nil];
        return [fileText componentsSeparatedByString:separation];
    }
    
    - (NSArray *)shuffledArray:(NSArray *)array {
        NSMutableArray *mutable = [array mutableCopy];
        NSMutableArray *result = [@[] mutableCopy];
        while ([mutable count]) {
            NSInteger index = arc4random()%[mutable count];
            [result addObject:mutable[index]];
            [mutable removeObjectAtIndex:index];
        }
        return result;
    }
    
    // not sure the context here, but ....
    NSArray *array = [self arrayFromFile];
    NSArray *shuffled = [self shuffledArray:array];
    for (NSString *string in shuffled) {
        // string will be random here
    }
    

    修改

    现在,在更多上下文的情况下,当按下按钮时,如何让用户在UILabel中看到随机字符串:

    将数组属性和当前索引添加到包含按钮和标签的视图控制器:

    @property(strong,nonatomic) NSArray *shuffledStrings;
    @property(assign,nonatomic) NSInteger currentIndex;
    

    创建一个"懒惰"数组的初始值设定项,使用上面建议的代码:

    - (NSArray *)shuffledStrings {
        if (!_shuffledStrings) {
            NSArray *array = [self arrayFromFile];
            _shuffledStrings = [self shuffledArray:array];
        }
        return _shuffledStrings;
    }
    

    OP没有定义当文件中的字符串全部呈现时应该发生什么。使用上面的工具,我们可以重新使用相同的洗牌列表,或者再次洗牌并重新开始......

    - (IBAction)buttonPressed:(id)sender {
        if (self.currentIndex == [self.shuffledStrings count]) {
            // just start again
            self.currentIndex = 0;
    
            // OR, reshuffle and start again
            // self.shuffledStrings = [self shuffledArray:self.shuffledStrings];
            // self.currentIndex = 0;
        }
        NSString *nextString = self.shuffledStrings[self.currentIndex++];
        // assuming you have a myLabel outlet
        self.myLabel.text = nextString;
    }