问题将URL(作为字符串)添加到数组

时间:2011-06-21 21:27:21

标签: iphone objective-c ios url loops

这是问题代码:

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

这就是代码所做的事情(至少应该这样做):

  1. 从URL(“4”)的内容生成一个整数。
  2. 使用for循环中的整数将对象添加到数组中。
  3. 谁能告诉我这里有什么问题?

    非常感谢,

    杰克

1 个答案:

答案 0 :(得分:2)

如果您想将网址添加为字符串,则不应该这样 -

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