簡體   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