This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to convert from int to string in objective c: example code…
How to convert integer to string in Objective C?
NSArray *myArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3]];
Update for new Objective-C syntax:
NSArray *myArray = #[#1, #2, #3];
Those two declarations are identical from the compiler's perspective.
if you're just wanting to use an integer in a string for putting into a textbox or something:
int myInteger = 5;
NSString* myNewString = [NSString stringWithFormat:#"%i", myInteger];
If you really want to use String:
NSString *number = [[NSString alloc] initWithFormat:#"%d", 123];
But I would recommend using NSNumber:
NSNumber *number = [[NSNumber alloc] initWithInt:123];
Then just add it to the array.
[array addObject:number];
Don't forget to release it after that, since you created it above.
[number release];
NSString* myNewString = [NSString stringWithFormat:#"%d", myInt];
Related
This question already has answers here:
How to convert numbers into text?
(8 answers)
Closed 7 years ago.
I'm looking for an algorithm or function to convert integer number 0,1,2 to Zero,One,Two respectively. How can we do this in Objective-C ?
Apple has a lot of handy formatting functionality built in for many data types. Called a "formatter," they can convert objects to/from string representations.
For your case, you will be using NSNumberFormatter, but if you have an integer you need to convert it to an NSNumber first. See below example.
NSInteger anInt = 11242043;
NSString *wordNumber;
//convert to words
NSNumber *numberValue = [NSNumber numberWithInt:anInt]; //needs to be NSNumber!
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
wordNumber = [numberFormatter stringFromNumber:numberValue];
NSLog(#"Answer: %#", wordNumber);
// Answer: eleven million two hundred forty-two thousand forty-three
This is my code for 0 to 100 (You can update as per your requirement). WORKING PERFECTLY !!
-(NSDictionary *)algorithm
{
NSArray *myArray = #[#"Zero",#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine",#"Ten",#"Eleven",#"Twelve",#"Thirteen",#"Fourteen",#"Fifteen",#"Sixteen",#"Sevteen",#"Eighteen",#"Nineteen"];
NSArray *tensArray = #[#"Twenty",#"Thirty",#"Fourty",#"Fifty",#"Sixty"
,#"Seventy",#"Eighty",#"Ninety",#"One Hundred"];
NSMutableDictionary *numberStringDictionary = [[NSMutableDictionary alloc] init];
NSMutableArray *numberStringsArray = [[NSMutableArray alloc] init];
for(int i=0;i<=100;i++)
{
if(i<20)
{
[numberStringDictionary setObject:myArray[i] forKey:[NSString stringWithFormat:#"%d",i]];
[numberStringsArray addObject:myArray[i]];
NSLog(#"\n%#",myArray[i]);
}
else if(i%10==0)
{
[numberStringDictionary setObject:tensArray[i/10-2] forKey:[NSString stringWithFormat:#"%d",i]];
[numberStringsArray addObject:tensArray[i/10-2]];
NSLog(#"\n%#",tensArray[i/10-2]);
}
else
{
[numberStringDictionary setObject:[NSString stringWithFormat:#"%# %#",tensArray[i/10-2],myArray[i%10]] forKey:[NSString stringWithFormat:#"%d",i]];
[numberStringsArray addObject:[NSString stringWithFormat:#"%# %#",tensArray[i/10-2],myArray[i%10]]];
NSLog(#"%#",[NSString stringWithFormat:#"%# %#",tensArray[i/10-2],myArray[i%10]]);
}
}
return numberStringDictionary;
}
This question already has answers here:
Convert NSArray to NSString in Objective-C
(9 answers)
Closed 9 years ago.
I wish to know how to convert an NSArray (for example: ) into an Objective-C string (NSString).
Also, how do I concatenate two strings together? So, in PHP it's:
$variable1 = "string one":
$variable2 = $variable1;
But I need it in Objective-C
Possible duplication: Convert NSArray to NSString in Objective-C
Firstly, that is not PHP concatenation, This is:
$variable1 = "Hello":
$variable1 .= "World";
see: https://stackoverflow.com/a/11441389/1255945
Next, Stackoverflow isnt a personal tutor. You should only post here specific problems and provide as much code and information as you can, not just stuff thats basically saying "I cant be bothered to look myself, tell me".
I must admit I have done this myself so i'm not having a go at you, just trying to be polite as share my knowledge and experience
With that in mind, to convert an NSArray to NSString
Taken from: http://ios-blog.co.uk/tutorials/objective-c-strings-a-guide-for-beginners/
NSString * resultString = [[array valueForKey:#"description"] componentsJoinedByString:#""];
If you want to split the string into an array use a method called componentsSeparatedByString to achieve this:
NSString *yourString = #"This is a test string";
NSArray *yourWords = [myString componentsSeparatedByString:#" "];
// yourWords is now: [#"This", #"is", #"a", #"test", #"string"]
if you need to split on a set of several different characters, use NSString’s componentsSeparatedByCharactersInSet:
NSString *yourString = #"Foo-bar/iOS-Blog";
NSArray *yourWords = [myString componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#"-/"]
];
// yourWords is now: [#"Foo", #"bar", #"iOS", #"Blog"]
Note however that the separator string can’t be blank. If you need to separate a string into its individual characters, just loop through the length of the string and convert each char into a new string:
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]];
for (int i=0; i < [myString length]; i++) {
NSString *ichar = [NSString stringWithFormat:#"%c", [myString characterAtIndex:i]];
[characters addObject:ichar];
}
Hope this helps, and Good luck developing :)
I have stored a value 1/129600.0 in a plist as a string.
I am able to retrieve it as a string but when i am trying to convert it as a double i am getting it as 1.0.I have also tried CFString
NSString *value = [[self array]objectAtIndex:m];
double a = [value doubleValue];
NSLog(#"%#",value);
NSLog(#"%f",a);
and in log the returned values are
1/129600.0 and 1.0
This code works fine, I tried it in xCode:
NSString *equation = [[self array]objectAtIndex:m];
NSExpression *result = [NSExpression expressionWithFormat:equation];
NSNumber *a = [result expressionValueWithObject:nil context: nil];
NSLog(#"%#",result);
NSLog(#"%.10f",[a doubleValue]);
I guess 1/129600.0 is not a valid number.
Try to create an expression and create an NSNumber from it:
NSString *equation = [[self array]objectAtIndex:m];
NSNumber *a = [[NSExpression expressionWithFormat:equation] expressionValueWithObject:nil context:nil];
double a = [result doubleValue];
NSLog(#"%f", a);
1/129600.0 is not a valid representation for a number in most programming languages, including ObjC. You need to parse the string and interpret it yourself.
Try this
NSString *value = [[self array]objectAtIndex:m];
NSArray *arr = [value componentsSeparatedByString:#"/"];
double a;
if ([arr count] == 2)
{
a = [arr objectAtIndex:0]/[arr objectAtIndex:1];
}
NSLog(#"%#",value);
NSLog(#"%f",a);
Is there any simple way how to initialize String in Objective-C with int such as in Java:
String myStr = 42 + "";
or I have to do
[NSString stringWithFormat:#"%d", 42];
everytime?
You could also use the NSNumber class for that:
NSNumber *number = [[NSNumber alloc] initWithInteger: val];
NSString *string = [number stringValue];
Perhaps not shorter, but it could be eventually faster.
Also you could create as said a helper method, than you wouldn't have to use more code than with the stringWithFormat: method.
Yes you have to do
[NSString stringWithFormat:#"%d", 42];
for Integer to string conversion.
Using a constant, like 42 in your example, you can write
NSString *myString = #"42";
Using a variable or expression, you can write
[NSString stringWithFormat:#"%d", myValue];
So I have an NSArray "myArray" with NSNumbers and NSStrings. I need them in another UIView so i go like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *details = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
details.subjectText = [[myArray objectAtIndex:indexPath.row] objectForKey:#"subject"];
The subjectText works.
But how can I get the NSNumbers out of it? (I actually need them as strings...)
I would convert a NSString out of a NSNumber like this:
NSString *blah = [NSNumber intValue]. But I don't know how to set it up in the code above...
Try:
NSString *myString = [NSNumber stringValue];
You can do it with:
NSNumber *myNumber = #15;
NSString *myNumberInString = [myNumber stringValue];
//An example of implementation :
// we set the score of one player to a value
[Game getCurrent].scorePlayer1 = [NSNumber numberWithInteger:1];
// We copy the value in a NSNumber
NSNumber *aNumber = [Game getCurrent].scorePlayer1;
// Conversion of the NSNumber aNumber to a String with stringValue
NSString *StringScorePlayer1 = [aNumber stringValue];
or try NSString *string = [NSString stringWithFormat:#"%d", [NSNumber intValue], nil];
The funny thing is that NSNumber converts to string automatically if it becomes a part of a string. I don't think it is documented.
Try these:
NSLog(#"My integer NSNumber:%#",[NSNumber numberWithInt:184]);
NSLog(#"My float NSNumber:%#",[NSNumber numberWithFloat:12.23f]);
NSLog(#"My bool(YES) NSNumber:%#",[NSNumber numberWithBool:YES]);
NSLog(#"My bool(NO) NSNumber:%#",[NSNumber numberWithBool:NO]);
NSString *myStringWithNumbers = [NSString stringWithFormat:#"Int:%#, Float:%# Bool:%#",[NSNumber numberWithInt:132],[NSNumber numberWithFloat:-4.823f],[NSNumber numberWithBool:YES]];
NSLog(#"%#",myStringWithNumbers);
It will print:
My integer NSNumber:184
My float NSNumber:12.23
My bool(YES) NSNumber:1
My bool(NO) NSNumber:0
Int:132, Float:-4.823 Bool:1
Works on both Mac and iOS
This one does not work:
NSString *myNSNumber2 = [NSNumber numberWithFloat:-34512.23f];
In Swift you can do like this
let number : NSNumber = 95
let str : String = number.stringValue
In Swift 3.0
let number:NSNumber = 25
let strValue = String(describing: number as NSNumber)
print("As String => \(strValue)")
We can get the number value in String.