Int / float for NSString without using alloc?

Is there a way to get the int (or float) of a number in an NSString object without using alloc and the subsequent version?

int myInt = 25; NSString *myString = [[NSString alloc] initWithFormat:@"%d",myInt]; ... [myString release]; 

EDIT:

Thanks for the answers, I should have been a little more clear in the question, I am especially interested in using this on the iPhone. As @chuck stated that I could use the convenience method, but I got the impression that I should avoid them, if possible, on the iPhone for reasons of memory / performance. Maybe I'm wrong.

Gary

+4
source share
7 answers

I could use a convenient method, but I got the impression that I should avoid them, if possible, on the iPhone for reasons of memory / performance.

"Reason for efficiency" is the cost of the implementation of the object. It was once expensive on the iPhone. (I donโ€™t know if this is all.) The only alternative is to clearly isolate it and free it yourself. As others have pointed out, you cannot have an object without selecting it, and you must not select it without releasing it. You need both things to happen one way or another.

The reason for the memory is that the auto-implemented object lasts longer, especially until an abstract is generated. Autorelease many objects, and your memory usage will accumulate; accumulate it high enough, and SpringBoard will tell your application to bring it down (and / or just kill it). The solution is to make the objects short-lived, which means either (1) creating and deleting your own pool of autoresists around a known batch of objects, or (2) managing the objects' lifetime (i.e. distributing and releasing them) yourself.

This last reason is not iPhone-specific - it also affects the Mac, although our ceiling is higher: the Mac has shorter memory and virtual memory, so we can get rid of more memory usage. However, we Mac programmers should also try not to waste memory, partly because the paging hells are destroyed one day, and partly because we will receive emails from users if our applications sit too high on the list Activity Monitor.

+3
source

It is not possible to create an NSString without creating it at some point. But you can use the convenience constructor so that you don't have a burden of ownership.

 NSString *myString = [NSString stringWithFormat:@"%d", myInt]; 

It will still be created and destroyed (as everything should be), but you do not have to do it yourself.

+16
source
 NSString *mystring = [NSString stringWithFormat:@"Hello: %d",myint]; 

it should be auto-implemented if you create it this way

I believe that the convention is that if you do not see the word init or alloc in the method name, then it should return an object with auto-implementation. that is, the object is added to the current auto resource pool and blurred when the application proceeds to the next phase of the life cycle.

+3
source

Do you want to

 NSString *aStr=[[NSNumber numberWithInt:myInt] stringValue]; 

It returns a string with auto-implementation.

+3
source

You can use NSMutableString (-appendFormat :) or a standard C-string. However, in principle, no, you will have to allocate memory somewhere.

+1
source

Why not define a macro ... something like

 #define intString(i1) [[[NSString alloc] initWithFormat:@"%d",i1] autorelease]; 

put it in the prefix header.

0
source

In most cases, autorelease will have zero effect on the overall memory usage of the application. You only need to take care of the author if you accumulate many instances of objects. For instance:

 for (int i = 0; i < 1000; ++i) { NSString* s = [NSString stringWithFormat:@"%d", i]; ... } 

In this example, at least 1000 different line instances will be accumulated before everything is released, which is undesirable. This is a situation when you will look for alternatives.

If you want to avoid creating a set of string instances, you can use NSMutableString :

 NSMutableString* s = [NSMutableString stringWithCapacity:20]; [s appendFormat:@"%d", 123]; ... [s setString:@""]; [s appendFormat:@"%d", 456]; ... 

The question arises as to whether this could be faster than just creating and releasing separate instances of strings, but this pattern may better match what you are trying to execute in your code.

0
source

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


All Articles