NSString stringwithstring 和 stringwithformat 区别
我真的很困惑,任何帮助都会受到高度赞赏
所以下面有什么区别
int myInteger = 1;
myString = [NSString stringWithFormat:@"anotesound%i",myInteger];
,
myString = [NSString stringWithString:@"anotesound1"];
我认为两者应该相同,但 open al 不接受它们是相等的,它适用于 stringwithstring 但不适用于 stringwithformat
I am really confused any help will be highly appreciated
So what is the difference below
int myInteger = 1;
myString = [NSString stringWithFormat:@"anotesound%i",myInteger];
and this
myString = [NSString stringWithString:@"anotesound1"];
I thought both should be same but open al do not accept those are equal, it works for the stringwithstring but not works for stringwithformat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
stringWithFormat:总是返回一个新创建的自动释放的对象,该对象最终会被释放并成为僵尸。 stringWithString:在这种情况下返回文字(常量)对象本身,该对象永远不会被释放。
我建议你学习iOS的内存管理,或者使用ARC。
http://developer.apple.com/library/ios/documentation/可可/概念/MemoryMgmt/
stringWithFormat: always returns a newly created and autoreleased object, which will eventually be released and become a zombie. stringWithString: in this case returns the literal (constant) object itself, which will never been released.
I'd recommend you to learn the memory management of iOS, or use ARC.
http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/
结果是一样的,错误在别处。
区别在于 myInteger 是否是常量或在不同实例化时填充变化。如果它总是相同,只需使用:
myString = @"anotesound1";
当字符串是常量时,不需要
stringWithString
方法。The results are the same, the error is elsewhere.
The difference is in wether or not myInteger is a constant or fill change on different instantiations. If it is always the same just use:
myString = @"anotesound1";
There is no need for the
stringWithString
method when the string is a constant.