Problem Adding URL (as string) to array
This is the problem code:
NSURL *url = [NSURL URLWithString:@"http://photostiubhart.comoj.com/ReadGallery/stiubhart1readgallery.php"];
NSError* error;
NSString* sizeString = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
double myDouble = [sizeString doubleValue];
int myInt = (int)(myDouble + (myDouble>0 ? 0.5 : -0.5));
//Create an array to hold the URLS
NSMutableArray *myURLS;
//Initialize the array with nil
myURLS = [[NSMutableArray alloc] init];
NSLog(@"Here1");
//Add all the URLs from the server to the array
for (int i = 0; i <= myInt; i++){
NSString *tempString = [[NSString alloc] initWithFormat : @"http://photostiubhart.comoj.com/GalleryImages/%dstiubhart1.jpg", i];
[myURLS addObject: [NSURL URLWithString:tempString]];
[tempString release];
}
myPhotos = [[NSMutableArray alloc] init];
for(int i = 0; i < myInt; i++){
[myPhotos addObject:[myURLS objectAtIndex:i]];
}
It gives the开发者_StackOverflow中文版 error:
2011-06-21 22:20:47.167 CHARLIE[666:207] -[NSURL length]: unrecognized selector sent to instance 0x70418c0
This is what the code does (at least supposed to):
- Generate an integer from the contents of the URL ("4").
- Use the integer in the for loops to add objects to arrays.
Can anyone tell me whats wrong here?
Thanks a lot,
Jack
If you want to add URLs as strings then shouldn't this –
for (int i = 0; i <= myInt; i++){
NSString *tempString = [[NSString alloc] initWithFormat : @"http://photostiubhart.comoj.com/GalleryImages/%dstiubhart1.jpg", i];
[myURLS addObject: [NSURL URLWithString:tempString]];
[tempString release];
}
be –
for (int i = 0; i <= myInt; i++){
NSString *tempString = [NSString stringWithFormat:@"http://photostiubhart.comoj.com/GalleryImages/%dstiubhart1.jpg", i];
[myURLS addObject:tempString];
}
You're probably expecting them to be strings and later calling length
method on them whereas they are stored as URLs in the array.
Additionally, myPhotos
seems to be a property in which case you can substitute -
myPhotos = [[NSMutableArray alloc] init];
for(int i = 0; i < myInt; i++){
[myPhotos addObject:[myURLS objectAtIndex:i]];
}
with this -
self.myPhotos = myURLS;
[myURLS release]; // To balance out the earlier alloc-init
精彩评论