Objective C: convert a NSMutableString in NSString

后端 未结 2 1489
闹比i
闹比i 2021-02-02 05:11

I have an NSMutableString, how can I convert it to an NSString?

2条回答
  •  长情又很酷
    2021-02-02 05:57

    NSMutableString is a subclass of NSString, so you could just typecast it:

    NSString *string = (NSString *)mutableString;
    

    In this case, string would be an alias of mutalbeString, but the compiler would complain if you tried to call any mutable methods on it.

    Also, you could create a new NSString with the class method:

    NSString *string = [NSString stringWithString:mutableString];
    

提交回复
热议问题