Проблема с добавлением URL (в виде строки) в массив - PullRequest
0 голосов
/ 22 июня 2011

Это код проблемы:

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 ЧАРЛИ [666: 207] - [Длина NSURL]: нераспознанный селектор отправлен на экземпляр 0x70418c0

Вот что делает код (по крайней мере, должен):

  1. Создание целого числа из содержимого URL («4»).
  2. Используйте целое число в циклах for для добавления объектов в массивы.

Может кто-нибудь сказать мне, что здесь не так?

Большое спасибо,

Jack

1 Ответ

2 голосов
/ 22 июня 2011

Если вы хотите добавить 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];
}

be -

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...