[英]problem with accessing NSMutableArray
我有以下問題:
int index=6;
imageView.image=[imageArray objectAtIndex:index];
NSLog(@"%@",[imageArray objectAtIndex:index]);
如果我運行此代碼,我會得到(null)作為 output ......即使我已經使用以下代碼很好地將圖像放入數組中:
NSURL *url = [NSURL URLWithString:@"somelink"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
[imageArray addObject:image];
我很確定有 20 張圖像(我使用 XML 文件並打印 URL 和圖像)並且圖像也很好。 我在放入數組之前打印圖像的值,這是我得到的值:
<UIImage: 0x5368670>
任何人都可以幫助我嗎? 謝謝。
請記住,您需要創建 NSMutableArray 的新實例......您可能只是在 nil 上調用方法。
在開始使用 imageArray 之前,請確保執行以下操作:
imageArray = [NSMutableArray array];
// or = [[NSMutableArray alloc] init] if you want to "retain" it
// for use in other methods
但是,您是否向數組中添加了至少七個這樣的對象? 請記住, NSArray
(和朋友)從零開始計數,而不是從一開始。
另外,您確定您添加的 object 不是 null (即dataWithContentsOfURL:
和imageWithData:
都成功)?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.