【问题标题】:'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array''NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:空数组的索引 1 超出范围'
【发布时间】:2012-05-25 05:44:24
【问题描述】:

我创建了一个从互联网下载音乐的类,当我在完成第一首歌曲下载后尝试下载歌曲时,我收到了错误

 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'

我的代码在这里,

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  [connection release];


    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

//[responseData release];

  dict=[responseString JSONValue];
  //title is  NSArray object
  title=[dict valueForKey:@"sTitle"];




  URLTitle=[[NSMutableArray alloc]init];
  [URLTitle addObjectsFromArray:title];



   [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectoryPath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"pslamsSongs.mp3"];
   NSLog(@"path %@",documentsDirectoryPath);
   [receivedData writeToFile:documentsDirectoryPath atomically:YES];

if (receivedData) {
     UIAlertView *alt = [[UIAlertView alloc] initWithTitle:@"Download Completed" message:@"Download Music completed, you can access the music from iTunes" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alt show];
    [alt release];
   }


 }




 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
   {


  return [URLTitle count];
   }

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
newCell = nil;

newCell = [tableView dequeueReusableCellWithIdentifier:identifier];

if(newCell == nil)
{
    NSLog(@"newCell ===================");
    NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"downloadPageCell" owner:self options:nil];
    newCell  = [ nibViews lastObject];
}

   newCell.downloadButton.tag=indexPath.row;


  [newCell.downloadButton addTarget:self action:@selector(downloadButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

newCell.downloadButton.tag=indexPath.row;


return newCell;
  }

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {

 }

  -(void)downloadButtonPressed:(UIButton*)sender
    {

  UIButton *btn1 = (UIButton *)sender;

 //**i got the error in this line**

   appendString=[URLTitle objectAtIndex:btn1.tag];


  appendString= [appendString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];




 URL= [staticURL stringByAppendingString:appendString];

  NSLog(@"download button pressed");
  downloadURL=[[NSURL alloc]initWithString:URL];

  theRequest = [NSURLRequest requestWithURL:downloadURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
  receivedData = [[NSMutableData alloc] initWithLength:0];
  connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];



  }

当我完成第一个下载后单击下一个下载时,我的数组变为空,有什么解决方案吗?

【问题讨论】:

    标签: nsurlconnection nsrangeexception


    【解决方案1】:

    每次下载完歌曲时,都会分配一个新的 NSMutableArray,将其分配给 URLTitle,然后将新下载的对象添加到那里。它与向现有的 NSMutableArray 添加对象不同,因此每次下载新歌曲时它都会首先变为空。

    尝试在 viewDidLoad 方法中只分配一次 URLTitle:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        URLTitle = [[NSMutableArray alloc]init];
    }
    

    并在 connectionDidFinishLoading 方法中保持[URLTitle addObjectsFromArray:title]; 不变。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多