0

http://www.raywenderlich.com/21320/objectively-speaking-a-crash-course-in-objective-c-ios6中有一个属性列表的剪切和粘贴 XML 版本。我有以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:@"quotes" ofType:@"plist"];
    self.movieQuotes = [NSMutableArray arrayWithContentsOfFile:plistCatPath];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)quoteButtonTapped:(id)sender {
    int array_total = [self.movieQuotes count];
    int index = (arc4random() % array_total);
    NSString *my_quote = self.movieQuotes[index][@"quote"];
    self.quoteText.text = [NSString stringWithFormat:@"Quote:\n\n%@", my_quote];
}

quoteButtonTapped 中的倒数第三行崩溃了,因为我试图取模数 0。这意味着 self.movi​​eQuotes 注册为空。

quotes.plist存储在目录的根目录中,它看起来与教程中的一样,以一个注释和空格为模。

有什么想法我正在做空self.movieQuotes吗?

4

1 回答 1

0

确保存储数组的属性的内存管理语义是strongretaincopy. 请注意,如果您使用复制语义,如果属性类型为NSMutableArray. (从您的示例中不清楚您是否真的期望或需要数组是可变的。)

当然,您还应该仔细检查以确保您有一个名为的格式良好的 plist 文件,该文件以quotes.plist数组作为其根元素,并且已正确添加到项目的 Copy Files 构建阶段。

编辑

最后,通过添加断点或语句确保您的viewDidLoad方法实际上正在被调用。NSLog

于 2013-08-28T23:15:15.140 回答