copenndthagen
copenndthagen

Reputation: 50732

int to NSString issue

I use the following statement for into to NSString conversion (with a find/replace)

curr_rep_date = [tmpRptDt stringByReplacingOccurrencesOfString:tmpYrVal withString:[NSString stringWithFormat:(tmpCurrYearInt-1)]];

I have declared

int tmpYrVal;
NSMutableString *tmp_dt,*curr_rep_date;

But the program seems to be crashing and the debugger is not giving any hint.

Could someone help me with the issue and what would be the correct usage.

Upvotes: 0

Views: 448

Answers (4)

Jasarien
Jasarien

Reputation: 58448

There's a number of problems here.

Firstly, sringByReplacingOcurrancesOfString:withString: is expecting NSStrings as parameters, not ints. That's the reason why it crashes. The method Is attempting to send a message to a primitive type, not an object.

Secondly, you need to use a proper format string for the stringWithFormat: method. This is the same as how NSLog works.

A format string can look like @"some text %d". It would then be followed by a comma separated list of values to be used in place of the % placeholders.

Example:

[NSString stringWithFormat:@"%d", myIntValue];

Will effectively turn your int into a string, as it creates a new string with a format using your int.

Upvotes: 1

Felix
Felix

Reputation: 35384

Basic int to NSString conversion works like this:

NSString* s = [NSString stringWithFormat:@"%d", intNumber];

Upvotes: 0

seppo0010
seppo0010

Reputation: 15849

You are missing the format


curr_rep_date = [tmpRptDt stringByReplacingOccurrencesOfString:tmpYrVal withString:[NSString stringWithFormat:@"%d", (tmpCurrYearInt-1)]];

Upvotes: 0

Philipp
Philipp

Reputation: 1963

You invoked the stringWithFormat - Method without a format string. [NSString stringWithFormat: @"%i", (tmpCurrYearInt-1)] should solve your problem.

Upvotes: 1

Related Questions