0

我这里的代码有问题

Book *theNewBook = [self parseTheBookXML];
// The book is not nil here
NSLog(@"The book's title: %@, number of pages:%@ and author: %@",theNewBook.title, theNewBook.pages, theNewBook.author);

[_theBooksArray addObject:theNewBook];

// TEST
Book *testBook = [_theBooksArray objectAtIndex:0];
// The book is nil here
NSLog(@"The book's title: %@, number of pages:%@ and author: %@",testBook.title, testBook.pages, testBook.author);

谁能告诉我为什么我的书对象是“零”,因为我在这里撞到了墙……

4

3 回答 3

1

正如您从对我的问题的评论中看到的那样,我的问题是我没有初始化我正在访问的数组。所以代替

[_theBooksArray addObject:theNewBook];

打电话

_theBooksArray = [[NSMutableArray alloc] initWithObjects:theNewBook, nil];

会成功的。

于 2013-01-24T14:09:48.150 回答
0
[_theBooksArray addObject:theNewBook];
[_theBooksArray count];

打印日志并检查数组计数。

于 2013-01-24T13:26:19.040 回答
0

您必须在使用之前初始化数组。

_theBooksArray = [[NSMutableArray alloc] init];

于 2013-01-25T07:20:34.940 回答