iphone - 将 URL(作为字符串)添加到数组时出现问题

标签 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 循环中的整数将对象添加到数组。

谁能告诉我这里出了什么问题?

非常感谢,

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/

相关文章:

iphone - 自定义 tableView 标题导致单元格边框显示

ios - 在 post 方法中上传音频和附加参数

ios - 将 WebView 与 Firebase 数据库结合使用

ios - 关于 MPMediaItemCollection 项目 lastObject

iphone - CGRect 在动画期间不改变

iphone - 怎么用闪光灯模式打开相机

iOS/iPhone - 快速拍摄或连拍照片

objective-c - 驱动文件更新错误 500

iOS - FileManager 扩展的最佳实践

ios - 如何使用 Core Data 的选项和 "None"选项创建 Eureka AlertRow?