Hi everybody,
i have a problem using dataWithContentsOfURL
.
I'm loading some images from the internet in a loop.
The Problem is: if the image at the URL doesn't exist, dataWithContentsOfURL
does NOT return nil as expected. The apple reference says it returns nil if NSData
could not be created.
Here's the Code:
NSString *TermineImgFileName = nil;
NSString *TermineImgPath = nil;
NSURL *TermineImgURL = nil;
NSData *TermineImg = nil;
for (deviceTermineHighInt; deviceTermineHighInt <= serverTermineHighInt; deviceTermineHighInt++) {
TermineImgFileName = [NSString stringWithFormat:@"Termine%i.png", deviceTermineHighInt];
TermineImgURL = [rootURL URLByAppendingPathComponent:TermineImgFileName];
TermineImg = [NSData dataWithContentsOfURL:TermineImgURL];
if (TermineImg != nil) {
TermineImgPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:TermineImgFileName];
[TermineImg writeToFile:TermineImgPath atomically:YES];
updateCount += 1;
NSLog(@"File %@ saved", TermineImgFileName);
}
else {
NSLog(@"Write Error");
}
TermineImg = nil;
}
Do you know why the method doesn't return nil if the file at the URL doesn't exist?
And a second question: Does it make sense to use the Strings, NSURL
and NSData
as i did? I thought for memory reasons it would be the best way.
Thank you in advance, Nikos
Edit: The variables for the loop are defined before the code, the loop works fine. Also the variable rootURL
is a constant defined in the header. The URL is built fine and it works.