I want to convert a random int number in the NSString
and then assign the NSString to another NSString
but it crashes the app
I am doing the following
int mynumber =(arc4random() % 1000 );
unique = [NSString stringWithFormat:@"%d",mynumber];
NSLog(unique)
NSString*test=unique;
it gives crash when i write last line;
It also prints values when I nslog the unique
string.
If you want to change the int to string
NSString *strFromInt = [NSString stringWithFormat:@"%d",yourintvalue];
This also works:
NSString *str = [@(number) stringValue];
Or, if you prefer dot notation:
NSString *str = @(number).stringValue;
This will box the primitive value to a NSNumber
using the expression boxing syntax @(...)
, then use its' stringValue
method to convert it to NSString
. This should also work for other primitive values (NSInteger
, float
, double
, long
, ...).
NSString *anotherStr;
int myNumber = (arc4random() % 1000 );
NSString *stringNum = [NSString stringWithFormat:@"%i", myNumber];
anotherStr = stringNum; //assign NSString to NSString
// Here you can convert NSString to Int if you want.
NSLog(@"My number as NSString = %@", stringNum);
int getNumFromString = [stringNum intValue];
NSLog(@"My number from NSString = %i", getNumFromString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With