繁体   English   中英

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

[英]how to replace the part of string from NSMutablestring

我正在开发一个应用程序。因为我从数组中取出字符串并替换。xml 从该字符串和 go 到下一页。如果我回来并执行该代码,应用程序将在该替换字符串行崩溃。下面是我的代码.

  -(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];
      }

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

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

尝试替换此行:

   NSMutableString *s1=[listoflessons objectAtIndex:i];

有了这个:

   NSMutableString *s1=[[NSMutableString alloc] initWithString:[listoflessons objectAtIndex:i]];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM