NSString 上的 stringWithFormat 与 initWithFormat [英] stringWithFormat vs. initWithFormat on NSString

查看:44
本文介绍了NSString 上的 stringWithFormat 与 initWithFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以这种方式声明 NSString 有什么区别,例如缺点和/或优点:

I am wondering what differences such as disadvantages and/or advantages there are to declaring an NSString this way:

NSString *noInit = [NSString stringWithFormat:@"lolcatz %d", i];

相反:

NSString *withInit = [[NSString alloc] initWithFormat:@"Hai %d", i];

使用 stringWithFormat 而不是仅仅使用 initWithFormat 方式来初始化字符串的动机是什么?

What was the motivation of putting stringWithFormat instead of just having the initWithFormat way of initializing the string?

推荐答案

stringWithFormat: 返回一个自动释放的字符串;initWithFormat: 返回一个必须由调用者释放的字符串.前者是一种所谓的方便"方法,对短期字符串很有用,因此调用者不必记住调用release.

stringWithFormat: returns an autoreleased string; initWithFormat: returns a string that must be released by the caller. The former is a so-called "convenience" method that is useful for short-lived strings, so the caller doesn't have to remember to call release.

这篇关于NSString 上的 stringWithFormat 与 initWithFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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