这是问题代码:
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]];
}
它给出了错误:
2011-06-21 22:20:47.167 CHARLIE[666:207] -[NSURL 长度]:发送到实例 0x70418c0 的无法识别的选择器
这就是代码的作用(至少应该如此):
- 根据 URL 的内容生成一个整数(“4”)。
- 使用 for 循环中的整数将对象添加到数组。
谁能告诉我这里出了什么问题?
非常感谢,
jack
最佳答案
如果您想将 URL 添加为字符串,那么不应该这样做 –
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];
}
是——
for (int i = 0; i <= myInt; i++){
NSString *tempString = [NSString stringWithFormat:@"http://photostiubhart.comoj.com/GalleryImages/%dstiubhart1.jpg", i];
[myURLS addObject:tempString];
}
您可能期望它们是字符串,然后在它们上调用 length
方法,而它们作为 URL 存储在数组中。
另外,myPhotos
似乎是一个属性,在这种情况下您可以替换 -
myPhotos = [[NSMutableArray alloc] init];
for(int i = 0; i < myInt; i++){
[myPhotos addObject:[myURLS objectAtIndex:i]];
}
有了这个-
self.myPhotos = myURLS;
[myURLS release]; // To balance out the earlier alloc-init
关于iphone - 将 URL(作为字符串)添加到数组时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6432211/