xcode NSNumber numberWithFloat vs Init 和 alloc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9073367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSNumber numberWithFloat vs Init and alloc
提问by dingerkingh
I have this line of code and am trying to figure out the pros and cons of the way I wrote it. I am just trying to set a label to a float value and both work.... just don't know which is better...
我有这行代码,并试图找出我编写它的方式的优缺点。我只是想将标签设置为浮点值并且两者都有效......只是不知道哪个更好......
self.display.text=[[NSNumber numberWithFloat:32.445] stringValue];
Is there any difference to say
有什么不同要说
NSNumber *number = [[NSNumber alloc]initWithFloat:32.445];
self.display.text = [number stringValue];
Well - I know there must be a difference - just not sure what it would be. Seems like the first is more of a wrapper (if that makes sense)?
嗯 - 我知道肯定有区别 - 只是不确定它会是什么。似乎第一个更像是一个包装器(如果有道理的话)?
Thanks!!!
谢谢!!!
回答by Julien
[NSNumber numberWithFloat:32.445]
is equivalent to:
相当于:
[[[NSNumber alloc] initWithFloat:32.445] autorelease]
in Manual Reference counting mode. In ARC or GC mode, you can consider it equivalent to:
在手动参考计数模式下。在 ARC 或 GC 模式下,你可以认为它等价于:
[[NSNumber alloc] initWithFloat:32.445]
Only benefit you might likely get is to try to avoid the autorelease
call in MRC mode and replace it with a release
call.
您可能获得的唯一好处是尽量避免autorelease
在 MRC 模式下调用并将其替换为release
调用。
回答by justin
Seems like the first is more of a wrapper (if that makes sense)?
似乎第一个更像是一个包装器(如果有道理的话)?
That is exactlywhat it is in the majority of implementations, and barring corner cases =p It's called a convenience constructor.
这正是大多数实现中的情况,并且除非极端情况 =p 它被称为便利构造函数。
As far as which you should prefer - whatever's clearer to you. I prefer alloc
+init
because it's easier to manage memory and requires slightly less overhead (apart from corner cases).
至于你应该更喜欢哪个 - 任何对你来说更清楚的。我更喜欢alloc
+init
因为它更容易管理内存并且需要的开销略少(除了极端情况)。
If you know you have a lot of objects to make or are writing performance/memory critical code (coughiOS devices), then you should consider favoring alloc+init for your 'heavier' code.
如果你知道你有很多对象要创建或正在编写性能/内存关键代码(咳iOS 设备),那么你应该考虑为你的“重”代码使用 alloc+init。
回答by Chris Chen
If you're working with ARC, then I suppose both ways are the same.
如果您正在使用ARC,那么我想两种方式都是一样的。
If you're working without ARC, like most of obj-c developers have been through, then your second snippet leaks memory, you may consider writing like this:
如果您在没有 ARC 的情况下工作,就像大多数 obj-c 开发人员一样,那么您的第二个代码段会泄漏内存,您可以考虑这样编写:
NSNumber *number = [[[NSNumber alloc]initWithFloat:32.445] autorelease];
回答by sblom
The numberWithFloat:
selector calls a class method on NSNumber (much like a static method from Java, C#, or C++). The initWithFloat:
selector calls an instance method on a specific NSNumber object. You use [NSNumber alloc]
to get the instance to init.
在numberWithFloat:
选择调用的NSNumber一个类的方法(很像从Java,C#或C ++的静态方法)。所述initWithFloat:
选择器调用一个特定的NSNumber对象上的实例方法。你[NSNumber alloc]
用来让实例初始化。
(See Apple's docsfor more.)
(有关更多信息,请参阅Apple 的文档。)
It's definitely fair to think of the static method as a wrapper for the second form, because somebody somewhere must be calling alloc
.
将静态方法视为第二种形式的包装器绝对是公平的,因为某个地方肯定有人在调用alloc
.
回答by Conrad Shultz
Unless you are running under ARC, you need to [number release] in the latter case. For a one-off use, then, the former case - using +numberWithFloat: - is probably preferable (less typing = fewer bugs, clearer code).
除非你在 ARC 下运行,否则你需要在后一种情况下 [number release]。对于一次性使用,前一种情况 - 使用 +numberWithFloat: - 可能更可取(更少的输入 = 更少的错误,更清晰的代码)。
The only real difference I can think of is that if you are using these in certain performance-critical applications, especially where loops are involved, there are more complex memory optimization considerations when deciding whether to use class or instance methods. These however don't really apply here (presumably this is UI code), so just make your life simple and use +numberWithFloat:.
我能想到的唯一真正区别是,如果您在某些对性能至关重要的应用程序中使用这些,尤其是在涉及循环的情况下,则在决定是使用类方法还是实例方法时需要考虑更复杂的内存优化问题。然而,这些在这里并不真正适用(大概这是 UI 代码),所以只要让你的生活变得简单并使用 +numberWithFloat:。
回答by Jim Rhodes
With the alloc/initWithFloat method, I believe 'number' will have to be released somewhere.
使用 alloc/initWithFloat 方法,我相信 'number' 将不得不在某处发布。