views:

200

answers:

2

I want to load the image URL from my NSMutableArray. Here is my Code:

id path = (NSString *)[[stories objectAtIndex: storyIndex] objectForKey: @"icon"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];

If I use: id path = @"http://www.xzy.de/icon.png"; it´s all right, but not if I want to extract the imageURL from my Array

Anyone who can help me? Thanks!

+1  A: 

I don't get why you declare path as anonymous id type. Define it as NSString. Shouldn't be any problems.

Eimantas
If I do so,I get following error:2010-03-28 13:38:32.816 Presentation[52185:20b] *** -[NSConcreteData dataWithContentsOfURL:]: unrecognized selector sent to instance 0x3d0d1a02010-03-28 13:38:32.816 Presentation[52185:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSConcreteData dataWithContentsOfURL:]: unrecognized selector sent to instance 0x3d0d1a0'2010-03-28 13:38:32.817 Presentation[52185:20b] Stack: ( ...)
pbcoder
That error.... is completely unrelated to the type declaration. Here, you're trying to use `dataWithContentsOfURL:` on an instance of NSData instead of on NSData itself.
codewarrior
A: 

Okay, here's the solution to the problem:

path = [path stringByReplacingOccurrencesOfString:@" " withString:@""];
path = [path stringByReplacingOccurrencesOfString:@"\n" withString:@""];
// that's a tab in the next line's string
path = [path stringByReplacingOccurrencesOfString:@"    " withString:@""];
pbcoder
Any idea why that was necessary?
Georg