- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的理解是这两个都创建了一个NSMutableString,只有第一个归系统所有,第二个归我所有(即我需要释放它)。我应该使用一个或另一个有什么特别的原因吗,从表面上看,使用第一个似乎更容易?第一个更好吗,因为它给了编译器一种大小感?
NSMutableString *newPath = [NSMutableString stringWithCapacity:42];
或
NSMutableString *newPath = [[NSMutableString alloc] init];
编辑...还有
我看到很多声明写在两行上(即)
NSMutableString *newPath;
newPath = [NSMutableString stringWithCapacity:42];
我个人更喜欢单线,这是个人风格的另一个例子吗?
最佳答案
NSMutableString *newPath = [NSMutableString stringWithCapacity:42];
OR
NSMutableString *newPath = [[NSMutableString alloc] init];
Is there any particular reason why I should use one or the other, on the face of it it seems easier to use the first?
是的。除非有特殊原因,否则始终立即自动释放。
第一个原因是很容易忘记编写 release
消息。如果您在创建对象的同一条语句中自动释放对象(如 [[[... alloc] init] autorelease]
),忘记它就更难,忘记它也更明显.方便的工厂方法(例如 stringWithCapacity:
)会为您自动释放对象,因此就像您自己自动释放它一样,您不必担心稍后会释放它。
其次,即使您确实记得编写单独的 release
消息,也很容易不命中它。提前返回有两种方式:
NSString *str = [[NSString alloc] initWithString:@"foo"];
BOOL success = [str writeToFile:path atomically:NO];
if (!success)
return;
[str release];
并抛出或传播异常:
NSString *str = [[NSString alloc] initWithString:@"foo"];
//Throws NSRangeException if str is not in the array or is only in the array as the last object
NSString *otherStr = [myArray objectAtIndex:[myArray indexOfObject:str] + 1];
[str release];
“不这样做的具体原因”通常是您有一个创建大量对象的紧密循环,在这种情况下,您可能希望尽可能多地手动管理循环中的对象,以保持你的对象倒计时。但是,只有在您有证据表明这是您的问题时才这样做(无论是来自 Shark 的硬数字,来自 Instruments 的硬数字,还是只要循环运行足够长的时间,您的系统就会进入分页 hell )。
其他可能更好的解决方案包括将循环拆分为两个嵌套循环(外部循环为内部循环创建和排出自动释放池)并切换到 NSOperation。 (但是,请确保您对队列一次运行的操作数设置了限制——否则,you may make it even easier to go into paging hell。)
Also is the first better as it gives the compiler a sense of size?
更好,但不是那个原因。
对于编译器来说,它只是另一个类消息。编译器不知道也不关心它做了什么;对于它所知道和关心的一切,stringWithCapacity:
是向用户播放歌曲的消息。
它确实为 NSMutableString 提供了一个大小提示——该类将知道它最初可能想要分配多少字符存储空间。您从中获得的任何好处都可能很小(至少在 Mac 上如此),但如果您手边有这些信息,为什么不使用它呢?相反,我不会特意去计算它。
I see a lot a declarations written on two lines (i.e.)
NSMutableString *newPath;
newPath = [NSMutableString stringWithCapacity:42];Personally I prefer the one-liner, is this just another example personal style?
是的。但是,让变量未初始化存在一定的风险。如果您决定养成这种习惯,请务必打开“运行静态分析器”build设置。
关于objective-c - 定义 NSMutableString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1487731/
NSMutableString *str, *str1; //这里分配 我正在使用[str appendString:str1] 不工作。[str appendFormat:str1] 不工作。 那么
@property (nonatomic, weak) id a; @property (nonatomic, weak) id sa; 我有两个弱点。数组将在复制后释放。但是 NSString 没有
我有一个相当大的循环,它获取一个字符串,对其执行一些操作,然后再进入下一个循环。我最初是释放它然后重新分配它,但认为这有点浪费资源,但不知道如何清除它以重用它。 最佳答案 一种方法是[myString
我有为 MD5 munge 准备消息的 java 代码 private static char[] jimsCopyRight = { 'C'、'o'、'p'、'y'、'r'、'i'、'g'、'h'、
我有这个代码: NSMutableString *printHead = [NSMutableString stringWithFormat:@"%@, %@", firstTextField.tex
我在M文件开头声明了一个对象如下: NSMutableString *SomeString; 在一个方法中我尝试初始化它: SomeString = [NSMutableString stringW
我想创建一个字符串。根据条件,我必须附加一些额外的字符串。在这种情况下,首选使用哪个?是根据条件创建两个不同的字符串还是创建可变字符串以根据条件附加新字符串? 前 if(a==1) { Str
一直在玩弄仪器,但运气不太好,无法弄清楚如何解决此内存泄漏问题。 首先是代码: -(NSString *) randomizeHint:(NSString *) wordToShuffle{
我有以下将字符串数组转换为字符串的代码 -(NSString *) arrayToString:(NSMutableArray *) array { NSMutableString *strF
我制作了一个包含几个单词的数组,然后我将所有单词放入一个可变字符串中,然后将这个字符串的每个字符放入一个可变数组中。然而,“wordSplitted”是一个可变的字符数组,在模拟器中每次启动应用程序时
我在使用谓词过滤数组时遇到问题。我的代码是这样的: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
我正在尝试构建一个字符串数组,该数组将添加到 XML soap 请求中。 我有一个简单的循环来构建数组: MarcTagsList 是一个 NSArray 字符串,它包含值 ["82a","100a"
NSMutableString *str = [[NSMutableString alloc] init]; 假设 str 有权知道一个值“me”。 并且在单击按钮时我希望 str 的值被重置。即字符
我是 objective-c 的绝对新手 使用此代码 NSMutableString *teststring; [teststring appendString:@"hey"]; NSLog(test
将 Swift String 转换为 NSString 效果很好。 let string = "some text" let nsString = string as NSString 但是当我这样做
我知道类似的问题已经被一次又一次地问到,但我请求您耐心等待,因为我似乎找不到有帮助的答案。 我的应用程序存在漏洞,这让我抓狂。实际上,使用 Leaks 不会将它们报告为泄漏,但 ObjectAlloc
我正在使用 XMLParser 来解析一些 XML 数据,它使用 NSMutableString *resultString 来存储标记字符。在每个 (- parser: didStarElement
我以字典格式的形式向 nsmutablestring 添加一些文本内容,并且每个字典都由“,”(commos)分隔。但是当获取这个总字符串时,它会显示 (,) 到结尾的字符串。那么如何删除该可变字符串
我有一个很大的 NSMutableString(可能有数百兆字节),需要对其执行大量 rangeOfCharacterFromSet: 操作。 为了提高性能,我想让一个 NSOperationQueu
我有以下代码 NSMutableString *ibmcountryCodeLabel=[[NSMutableString alloc]init]; NSMutableString *imobileN
我是一名优秀的程序员,十分优秀!