“ - [NSMutableArray insertObject:atIndex:]:尝试将nil对象插入0'”
如果我使用硬编码路径(代码中的“路径”),则会正确添加图像。当我从RSS提要中检索相同的URL时,它不起作用(上面的错误)。
我尝试使用简单的相等性检查来检查URL,但它表明它们不相等,尽管它们在调试器输出中看起来是这样。
对内存管理的任何见解也会有所帮助。我来自Flash工作世界和一个菜鸟到xcode(抱歉)。我知道我需要停止将内存管理视为事后的想法......
-
- (void)viewDidLoad
{
[super viewDidLoad];
[self prepareGalleryLoad]; //sets up coverflow, waiting for data
NSString *xmlpath = @"http://mydomain.com/gallery3/index.php/rss/feed/gallery/album/12";
xmlreader = [[XMLReader alloc] init];
[xmlreader parseXMLFileAtURL:xmlpath];
NSMutableArray *imagearray = [[NSMutableArray alloc]init]; //array of images
NSMutableArray *imagearraycaptions = [[NSMutableArray alloc]init]; //array of captions for images
NSString *path;
NSString *pathfromrss;
UIImage *tempimage;
for (int y = 0; y < [xmlreader.stories count]; y++){
[imagearraycaptions addObject:[[xmlreader.stories objectAtIndex:y] objectForKey:@"title"]];
path = @"http://mydomain.com/gallery3/var/resizes/College-of-Nursing-and-Health-Studies/health10.jpg?m=1299713266";
pathfromrss = [NSString stringWithFormat:@"%@",[[xmlreader.stories objectAtIndex:y] objectForKey:@"link"]];
//DIAGNOSTIC check of path vs path temp.
if (path == pathfromrss){
NSLog(@"path is equal");
}else{
NSLog(@"path is not equal");
NSLog(@"%@",path);
NSLog(@"%@",pathfromrss);
}
//END DIAGNOSTIC path check
tempimage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:path]]];
//tempimage is NIL when using "pathfromrss", works fine with "path"
if(tempimage){
[imagearray addObject:tempimage];
}
}
coverstext = imagearraycaptions; //coverflow uses this
covers = imagearray; //coverflow uses this
[self finishGalleryLoad];
}
答案 0 :(得分:0)
一位朋友帮助我找到答案。 RSS提要包括我没有在调试器输出中捕获的换行符。用以下方法清理它们:
for (int y = 0; y < [xmlreader.stories count]; y++){
pathfromrss = [NSString stringWithFormat:@"%@",[[xmlreader.stories objectAtIndex:y] objectForKey:@"link"]];
[coverflowdescription setText:pathfromrss];
NSString *cleanurl = [pathfromrss stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:cleanurl]];
UIImage *tempimage = [[UIImage alloc] initWithData:mydata];
if(tempimage){
[imagearraycaptions addObject:[[xmlreader.stories objectAtIndex:y] objectForKey:@"title"]];
[imagearray addObject:tempimage];
}
}