如何从NSMutablestring中替换字符串的一部分

时间:2012-04-05 13:03:25

标签: iphone

我开发了一个应用程序。我从数组中获取字符串并从该字符串替换.xml并转到下一页。如果我回来执行该代码,应用程序将在该替换字符串行崩溃.Below是我的代码。

  -(void)open:(UITapGestureRecognizer*)recognizer
  {
       NSInteger i=(recognizer).view.tag;
       NSMutableString *s1=[listoflessons objectAtIndex:i];
       [default1 setObject:s1 forKey:@"KeyToXmlFile"];
       NSLog(@"%@",[default1 objectForKey:@"KeyToXmlFile"]);
        [s1 replaceCharactersInRange:[s1 rangeOfString: @".xml"] withString: @""];
        [default1 setObject:s1 forKey:@"KeyToSelectedFile"];
        [listoflessons removeObjectAtIndex:i];
      [listoflessons insertObject:[default1 objectForKey:@"KeyToXmlFile"] atIndex:i];
        NSLog(@"%@",listoflessons);
       SecondViewCOntroller *snd=[[SecondViewCOntroller    alloc]initWithNibName:@"SecondViewCOntroller" bundle:nil];
       [self.navigationController pushViewController:snd animated:YES];
      }

所以请告诉我如何避免那个。

1 个答案:

答案 0 :(得分:1)

我的猜测是“listoflessons”中的对象是NSString类型而不是NSMutableString。

尝试替换此行:

   NSMutableString *s1=[listoflessons objectAtIndex:i];

用这个:

   NSMutableString *s1=[[NSMutableString alloc] initWithString:[listoflessons objectAtIndex:i]];
相关问题