在NSMutableArray中获取对象

Dre*_*rew 13 objective-c

假设如下:

@interface BOOK : NSObject
{
    @private
    NSString *title;
    NSString *author;
    NSString *ISBN;
}
...
BOOK *booklist = [[[BOOK alloc] init] autorelease];
NSMutableArray *myarray = [NSMutableArray array];
while (true)
{
    booklist.title = @"title";
    booklist.author = @"author";
    booklist.ISBN = @"1234-1";
    [myarray addObject:booklist];
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何在myarray的某个索引处检索BOOK的对象,即booklist.title,.author,.ISBN.

Mat*_*uch 29

用这个代码?不可能,因为你在其余时间添加相同的bookList(直到没有更多的内存)

使用没有无限循环的代码

NSInteger index = 0;
Book *aBook = [myArray objectAtIndex:index];
NSString *title = aBook.title;
Run Code Online (Sandbox Code Playgroud)