How to initialize NSString to NSMutableString?

前端 未结 4 1356
半阙折子戏
半阙折子戏 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:33

    You can set an NSMutableString to an NSString, but not the other way around

    NSString *str =  [NSMutableString alloc]init];
    

    is okay, but

    NSMutableString *str = [[NSString alloc]init];
    

    is not. This is because an NSMutableString is a subclass of NSString, so it 'is a' NSString. You can however create a mutable string from an NSString with

    NSMutableString *mStr = [str mutableCopy];
    

提交回复
热议问题