NSNumber numberWithFloat vs Init and alloc

I have this line of code and am trying to figure out the pros and cons of how I wrote it. I'm just trying to set the label to float, 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 that there must be a difference - I'm just not sure what it will be. It seems that the first is more like a wrapper (if that makes sense)?

Thanks!!!

+4
source share
6 answers

 [NSNumber numberWithFloat:32.445] 

is equivalent to:

 [[[NSNumber alloc] initWithFloat:32.445] autorelease] 

in manual mode. In ARC or GC mode, you can consider it equivalent:

 [[NSNumber alloc] initWithFloat:32.445] 

The advantage you are likely to get is to try to avoid calling autorelease in MRC mode and replace it with calling release .

+9
source

Seems like the first is more of a wrapper (if that makes sense)?

This is exactly what is available in most implementations, and the prohibition of angular cases = p He called the constructor of convenience.

How much you prefer - how much clearer to you. I prefer alloc + init because it simplifies memory management and requires slightly less overhead (besides corner cases).

If you know that you have many objects for creating or writing critical performance / memory code (cough iOS devices), then you should consider using alloc + init for your heavier code.

+2
source

The selector numberWithFloat: calls the class method in NSNumber (as a static method from Java, C # or C ++). The selector initWithFloat: calls the instance method for a specific NSNumber object. You use [NSNumber alloc] to get an init instance.

(For more information, see Apple Docs .)

It is definitely fair to think of a static method as a wrapper for the second form, because someone must call alloc .

+1
source

If you are not running ARC, you need [release number] in the latter case. For a one-time use, the first case is the use of + numberWithFloat: - is probably preferable (less input = less errors, more clear code).

The only real difference I can think of is that if you use them in certain mission-critical applications, especially when loops are involved, when deciding whether to use class or instance methods, there are more complex memory optimization considerations. They are, however, really not applicable here (presumably this is UI code), so just make your life simple and use + numberWithFloat :.

+1
source

If you work with ARC , I assume that both paths are the same.

If you work without ARC, like most obj-c developers, your second piece of memory leak, you might think about how to write:

 NSNumber *number = [[[NSNumber alloc]initWithFloat:32.445] autorelease]; 
+1
source

Using the alloc / initWithFloat method, I believe that a number should be issued somewhere.

0
source

Source: https://habr.com/ru/post/1393878/


All Articles