+[NSString stringWithString:] -- 有什么意义? [英] +[NSString stringWithString:] -- what's the point?

查看:26
本文介绍了+[NSString stringWithString:] -- 有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于NSString 字符串是不可变的,stringWithString: 类方法的值是什么?

As NSString strings are immutable, what is the value of the stringWithString: class method?

当与 NSMutableString 一起使用时,我得到了该实用程序,我只是没有看到带有 NSString 类的实用程序.

I get the utility when used with NSMutableString, I just didn't see the utility with the NSString class.

推荐答案

你可能有一个想要复制的 NSMutableString(或一些本土的 NSString 子类).

You might have a NSMutableString (or some home-grown NSString subclass) that you want to duplicate.

NSMutableString *buffer = [NSMutableString string];
// do something with buffer
NSString *immutableStringToKeepAround = [NSString stringWithString:buffer];

当然,你也可以复制一份:

Of course, you can also just make a copy:

NSMutableString *buffer = [NSMutableString string];
// do something with buffer
NSString *immutableStringToKeepAround = [[buffer copy] autorelease];

但您拥有该副本并且必须发布或自动发布它.

but you own the copy and must release or autorelease it.

这篇关于+[NSString stringWithString:] -- 有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆