簡體   English   中英

IOS/Objective-C:NSArray 隨機化問題

[英]IOS/Objective-C: NSArray randomization issue

我有這個帶有字符串的數組,我想對其進行隨機化並將輸出顯示在標簽中。 但是使用這段代碼,我總是有相同的奇怪輸出,即“2”......知道我做錯了什么嗎?

- (void)viewDidLoad {
    [super viewDidLoad];
    self.quoteInput.delegate=self;
    self.quoteLabel=_quotationLabel;
    self.correctLabel.alpha=0;
    self.wrongLabel.alpha=0;
    
    _quoteInput.delegate=self;
    
    
    
   _levelOne = @[
                   [words Quotes:@"First Quote."],
                   [words Quotes:@"Second Quote."],
                 [words Quotes:@"And so on."]];
    
}

-(void)randomQuotes{

    for (NSInteger x = 0; x < [_levelOne count]; x++) {

        NSInteger randInt = arc4random() % ([_levelOne count] - x) + x;
        [_levelOne objectAtIndex:randInt];

        NSString *one = [NSString stringWithFormat:@"%d", randInt];
        self.quoteLabel.text = one;
    }
}
- (IBAction)generateQuote:(UIButton *)sender {
    [self randomQuotes];
}

編輯詞類:

 + (instancetype)Quotes:(NSString *)quotes
                       {
        

return [[words alloc] initWithQuotes:quotes];
}

除非你能解釋[word Quotes:]方法的作用,它應該比你想要做的簡單得多。

_levelOne = @[@"First Quote.", "Second Quote.", @"And so on."];

-(void) randomQuotes{ 
    NSInteger randInt = arc4random_uniform( [_levelOne count] );
    self.quoteLabel.text = [_levelOne objectAtIndex:randInt];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM