views:

531

answers:

8

Is there anyway to get int (or float) numbers into a NSString object without using alloc and a subsequent release?

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 particularly interested in using this on the iPhone. As @chuck stated I could use a convenience method, but I was under the impression that I should be avoiding these where possible on the iPhone for memory / performance reasons. I could be wrong though.

gary

+11  A: 

There's no way to create an NSString without creating it at some point. But you could use a convenience constructor so you don't have the burden of ownership.

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

It will still be created and destroyed (as everything must be), but you don't have to do it yourself.

Chuck
+3  A: 
NSString *mystring = [NSString stringWithFormat:@"Hello: %d",myint];

it should be autoreleased if you create it this way

i think the convention is that unless you see the word init or alloc in the method name then it should return an autoreleased object. i.e. the object is added to the current autorelease pool and flushed when the app advances to next stage in lifecycle

PeanutPower
+1  A: 

You could use an NSMutableString (-appendFormat:) or a standard C-string. However, fundamentally, no, you're going to have to allocate memory somewhere.

Ben Gottlieb
A: 

Why not define a macro... something like

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

put it in your prefix header.

+1  A: 

You want

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

It returns an autoreleased string.

TechZen
Interesting, I did not know about -(NSString *)stringValue
fuzzygoat
A: 

Most of the time autorelease will have zero impact on your app's overall memory usage. You only need to be concerned with autorelease if you're accumulating many object instances. For example:

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

That example will accumulate at least 1000 different string instances before anything is released, which is not desirable. That's a situation where you would look for alternatives.

If you wanted to avoid creating a bunch of string instances you could use NSMutableString:

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

It's questionable whether that's any faster than simply creating and releasing separate string instances, but that pattern may fit better with what you're trying to accomplish in your code.

Darren
+2  A: 

I could use a convenience method, but I was under the impression that I should be avoiding these where possible on the iPhone for memory / performance reasons.

The “performance reason” is the cost of autoreleasing an object. This was, at one time, expensive on an iPhone. (I don't know whether it still is.) The only alternative is explicitly allocating it and releasing it yourself. As others have pointed out, you can't have an object without having allocated it, and you mustn't allocate it without releasing it. You need both things to happen one way or another.

The memory reason is simply that an autoreleased object lasts longer—specifically, until the autorelease comes due. Autorelease many objects, and your memory usage will pile up; pile it up high enough, and SpringBoard will tell your app to knock it off (and/or just kill it). The solution is to make the objects not last so long, which means either (1) creating and draining your own autorelease pool around a known batch of objects or (2) managing the objects' lifetimes (that is, allocating and releasing them) yourself.

This latter reason is not specific to the iPhone—it affects the Mac as well, although our ceiling is higher: Macs have more short-term memory, plus virtual memory, so we can get away with more memory usage. Even so, we Mac programmers should also try not to waste memory, partly because paging hell wrecks one's day, and partly because we will get email from users if our apps sit too high in the Activity Monitor list.

Peter Hosey
Many thanks Peter, I fully understand now, much appreciated.
fuzzygoat
A: 

Hey Guys ,

I am totally new to iPhone programming .. So please don't be hard on me :P

Ok here is the problem

AdditionViewController.h

#import <UIKit/UIKit.h>

@interface AdditionViewController : UIViewController 
{
    IBOutlet UITextField *Number1;
    IBOutlet UITextField *Number2;
    IBOutlet UITextField *SumAnswer;
}

@property (retain, nonatomic) UITextField *Number1;
@property (retain, nonatomic) UITextField *Number2;
@property (retain, nonatomic) UITextField *SumAnswer;
-(IBAction)buttonPressed1:(id)sender;

@end

AdditionViewController.m

#import "AdditionViewController.h"

    @implementation AdditionViewController
    @synthesize Number1;
    @synthesize Number2;
    @synthesize SumAnswer;


    -(IBAction)buttonPressed1:(id)sender
    {
        SumAnswer.text = Number1.text + Number2.text; 
           // I want the above line to be edited so that I can get the addition
             of both the numbers in the 3rd Text Field 
    }

    -
    -
    -
    -
    -
    @end

I am really new to iPhone programming .. I am trying to understand how NSString can be used here .. But I am finding it a bit complicated .

If someone please tell me what the code should be ... It will be a great help . I have to submit a mini project to my school asap .

Thanking You , int3rc3pt0r enter code here

int3rc3pt0r
You might want to post this as a new question, rather than add it as an answer here. Do an edit and copy all your text, then click "Ask Question" in the upper right of the page and paste it there.
fuzzygoat