ios - `[Quotes objectAtIndex: quoteNumber]` 没有返回任何东西

标签 ios objective-c nsarray

我有这个小项目来帮助我适应 Objective-C 和 iOS 开发,它应该在屏幕上显示引号并每 20 秒或用户按下按钮时更改引号。为了实现这一点,我到目前为止有这段代码:

-(void) CreateArray {
    Quotes = [NSArray arrayWithObjects:
              @"Maybe, just once, someone will call me 'Sir' without adding, 'You're making a scene.'",
              @"You know, boys, a nuclear reactor is a lot like a woman. You just have to read the manual and press the right buttons.",
              @"When will I learn? The answer to life's problems aren't at the bottom of a bottle, they're on TV!",
              @"Son, when you participate in sporting events, it's not whether you win or lose: it's how drunk you get.",
              @"Please don't eat me! I have a wife and kids. Eat them!",
              @"Marriage is like a coffin and each kid is another nail.",
              @"Kids, you tried your best and you failed miserably. The lesson is, never try.",
              @"When I look at the smiles on all the children's faces, I just know they're about to jab me with something.",
              @"I want to share something with you: The three little sentences that will get you through life. Number 1: Cover for me. Number 2: Oh, good idea, Boss! Number 3: It was like that when I got here.",
              @"Oh, people can come up with statistics to prove anything, Kent. 14% of people know that.",
              @"Remember that postcard Grandpa sent us from Florida of that Alligator biting that woman's bottom? That's right, we all thought it was hilarious. But, it turns out we were wrong. That alligator was sexually harrassing that woman.",
              @"Kill my boss? Do I dare live out the American dream?",
              @"If something goes wrong at the plant, blame the guy who can't speak English.",
              @"Alcohol is a way of life, alcohol is my way of life, and I aim to keep it."];
}

-(void) nextQuote:(NSTimer *)timer {
    quoteNumber++;
    QuoteLabel.text = [Quotes objectAtIndex: quoteNumber];
}

-(IBAction)nextButton:(id)sender {
    quoteNumber++;
    NSString *Quote = [Quotes objectAtIndex: quoteNumber];
    QuoteLabel.text = Quote;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    timer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(nextQuote:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

我真的看不出这段代码有任何问题,但到目前为止我遇到了一个相当大的问题:只要引用应该更改,标签就会变成空白。我尝试使用 NSLog 查看 [Quotes objectAtIndex: quoteNumber] 返回的内容,但它没有记录任何内容。我也尝试过使用 Quotes[quoteNumber],但完全没有区别。

我是 Objective-C 和 iOS 开发的初学者,所以这可能有一个非常明显的解决方案,但我尝试在网上寻找它,但找不到任何对我有帮助的东西。

最佳答案

问题是您从未调用过 CreateArray,因此 Quotes 数组始终为空。

还有一些值得注意的事情。类名应以大写字母开头,方法/属性应以小写字母开头。

还有这些方法:

-(void) nextQuote:(NSTimer *)timer {
    quoteNumber++;
    QuoteLabel.text = [Quotes objectAtIndex: quoteNumber];
}

-(IBAction)nextButton:(id)sender {
    quoteNumber++;
    NSString *Quote = [Quotes objectAtIndex: quoteNumber];
    QuoteLabel.text = Quote;
}

您最终会导致索引越界,因为您永远不会重置 quoteNumber。类似

if(quoteNumber % self.Quotes.count == 0){
  quoteNumber = 0;
}

只要 quoteNumberQuotes 数组中的对象数量相同,就会重置它。

最后,什么是Quotes?是属性(property)吗?如果是这样,您应该选择重写 setter/getter 之类的东西

- (NSArray *)quotes
{
  if(!_quotes){
    _quotes = @[]; //Here is where you'd make your array with objects
}

这应该让你开始运行。

关于ios - `[Quotes objectAtIndex: quoteNumber]` 没有返回任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20181227/

相关文章:

ios - 由于加载时崩溃,Apple 拒绝了我的应用程序,我无法重现它。我应该怎么办?

objective-c - 在 objective-c 中存储单词列表

ios - 按 NSDate 作为内键对字典数组进行排序

ios - 如何将 CAShapeLayer 转换为 CAGradientLayer?

iphone - 仅当页码为零时隐藏上一个按钮,仅当页码为 20 时隐藏下一个按钮

ios - MPMusicPlayerController杀死iPhone扬声器路线上的RemoteIO

ios - 自定义单元格中的选定行不会将完整数据从segue传递到另一个 View Controller

ios - 访问 UITableViewController 单元格集合

ios - NSOperation 和 NSOperationQueue 工作线程与主线程

iphone - 字典值检索