在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.movieQuotes 注册为空。
quotes.plist
存储在目录的根目录中,它看起来与教程中的一样,以一个注释和空格为模。
有什么想法我正在做空self.movieQuotes
吗?