How to initialize NSString to NSMutableString?

前端 未结 4 1319
半阙折子戏
半阙折子戏 2020-12-18 05:41

Is there any way to initialize NSString to NSMutableString? and also reverse order?

-(void)st:(NSString *)st
{
  NSMutableString str = st; // gives warning..         


        
4条回答
  •  醉梦人生
    2020-12-18 06:23

    NSString is an immutable representation (or a readonly view at worst). So you would need to either cast to a NSMutableString if you know it's mutable or make a mutable copy:

    -(void)st:(NSString *)st
    {
      NSMutableString *str =  [[st mutableCopy] autorelease];
      NSLog(@"string: %@", str);
    }
    

    I autoreleased it because mutableCopy returns a newly initialized copy with a retain count of 1.

提交回复
热议问题