本文介绍了如何初始化NSString到NSMutableString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法来初始化NSString NSMutableString?还可以颠倒顺序?

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

-(void)st:(NSString *)st
{
  NSMutableString str = st; // gives warning..
  NSLog(@"string: %@", str);
}


推荐答案

NSString 是一个不可变的表示(或最坏的只读视图)。所以如果你知道它是可变的或者是一个可变的副本,你需要转换到 NSMutableString

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);
}

我自动释放它,因为 mutableCopy 返回保留计数为1的新初始化副本。

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

这篇关于如何初始化NSString到NSMutableString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:32