how to convert NSMutable array with NSNumber into strings [closed] - iphone

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
my array is containing ids they are in NSNumber how can i convert them in string my array is like below
1494447926,
1537064431,
1545735176,
1574825141,
1604834983,
1829486110,
1838260338,
1846543841,
1850381039,
100000039842949,
100000077723868,
100000103091995,
100000126558358,
100000130915431,
100000139092102,
100000157330187,
100000157646688,
100000197141710,
100000243178639,
100000249947961,
please give me sample code to convert it to string

First of all array can not store integer. It must be in NSNumber or it is in NSString itself.
In either of the case you can create a long string by appending them,
NSString *string=[yourArray componentsJoinedByString:#","];
Or, if you want each value as string then you need to create that much string and then access them.
NSArray *numbersToStrings=[NSArray new];
for(id element in yourArray){
[numbersToStrings addObject:[NSString stringWithFormat:#"%#",element];
}
Here numbersToStrings contains all the values as string.

Use this.
NSString *str = [NSString stringWithFormat:#"%i",number];

for(int i=0;i<[arr count];i++){
str = [NSString stringWithFormat:#"%d",[arr objectAtIndex:i]];
[newArr addObject:str];
}

NSString *str = [NSString StringWithFormat:#"%d",1494447926];

You can use stringWithFormat
NSString *str = [NSString stringWithFormat:#"%d", [YourArray objectAtIndex:index]];

You cannot store integers into an array. If you are getting this response from server each would be NSNumber. You can type cast that to NSString.

do this
NSArray *ll=[NSArray arrayWithObjects:#"1",#"2",#"3", nil];
NSString *strinList=[NSString stringWithFormat:#"%#",[ll objectAtIndex:0]];
try this ,if you required other help ,i am here .

Only Search on Google - convert int to NSString , multiple Answer are displayed
by the way, your need to use only [NSString stringWithFormat:#"%d",YourIntValue]
for (int i=0; i < MyArray.count; i++)
{
NSString * String =[NSString stringWithFormat:#"%d", [MyArray objectAtIndex:i]];
NSLog(#"%#",String);
}

Related

I would like to ask how to convert hex NSString to long value [duplicate]

This question already has an answer here:
How to get an int in decimal from a Hex NSString
(1 answer)
Closed 9 years ago.
for example i want to covert this to long
NSString *str = #"FFFF00FF";
thanks!
Try to use this one
NSString *str = #"FFFF00FF";
NSScanner* scanner = [NSScanner scannerWithString: str];
unsigned long long longValue;
[scanner scanHexLongLong: &longValue];
NSLog(#"LongValue = %lld", longValue);
i hope it may help you.
long *longString = [YourString longLongValue];
NSString *str = #"FFFF00FF";
long longstring=[str longLongValue];
NSLog(#"%ld",longstring);
try this one.mightbe help ful to you.

How to concatenate all values of array in Xcode [duplicate]

This question already has answers here:
Join an Array in Objective-C
(3 answers)
Closed 9 years ago.
I am new in iOS and i am not able to find solution of my question . So please tell me how can i concatenate all values of array in iOS
Answer : -componentsJoinedByString: .
It will join the components in the array by the specified string and return a string representation of the array.
Use : NSString *finalStr = [yourArray componentsJoinedByString:#" "];
GoodLuck !!!
if you are using string values in NSArray then it is quite simple.
You can just run for loop and use a NSMutableString to append the data.
NSArray *_array;//Your Initialized Array
NSMutableString *_string =[[NSMutableString alloc]init];
for(int i= 0 ;i<[_array count];i++)
{
[_string appendFormat:#"%#",[_array objectAtIndex:i]];
}
NSLog(#"_string = %#",_string);
NSMutableString* theString = [NSMutableString string];
for (int i=0; i<[array count];i++){
[theString appendString:[NSString stringWithFormat:#"%i ",i]];
}
NSLog(#"mystirng=%#", theString;);

NSMutableString append is not working [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Objective-C: How do you append a string to an NSMutableString?
I have a NSMutableArray. I want to extract all elements of this NSMutable array and append it in to a string. My following code is not working. String is not append to the variable. Also I am using automatic reference counting feature in this project.
NSMutableString *theString = [NSMutableString string];
for(int i=0; i<[self.incomingMessages count]; i++) {
id object = [self.incomingMessages objectAtIndex: i];
[theString appendString:(NSString*)object];
}
NSLog(#"the appended string: %#", theString);
[self.incomingMessages removeAllObjects];
How can I append the stored strings into the theString variable. Whats is the mistake I have done?
Do this
NSMutableString *theString = [[NSMutableString alloc]init ];
Try to print and see if your object is string
Maybe you need to initialize your theString like this?
NSMutableString *theString = [NSMutableString stringWithString:#""];

Transforming NSMutableArray values [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I've been looking for this, but can't find the answer.
I have an NSMutableArray with values who_1, what_2, where_3 etc.
I want to transform this into who, what, where etc.
I already have the value of the integer as a variable, and _ is just a string.
What steps should I take to have all these arrayvalues transformed?
NSArray * arrB = [[NSArray alloc]initWithObjects:#"apple_a",#"ball_b",#"cat_c",#"doll_d",nil];
NSMutableArray * arrA = [[NSMutableArray alloc]init];
for(NSString *strData in arrB)
{
NSArray *arr = [strData componentsSeparatedByString:#"_"];
[arrA addObject:[arr objectAtIndex:0]];
}
and this would be your output
arrA:(
apple,
ball,
cat,
doll
)
You need to apply logic for that, You cant find answers to tricky Questions :)
You need to run a loop.
Separate string with '_'
Loop
for(NSString *s in ary)
{
NSArray *a = [s componentsSeparatedByString:#"_"];
[anotherArray addObject:[a objectAtIndex:0]];
}
and update your array..
Following might help you -
NSRange range = [string rangeOfString:#"_"];
NSString *finalString = [originalString substringToIndex:range.location];
you can have this in loop.
Or you can go for componentSeperatedByStrings.
This might help you
NSMutableArray *tmpAry = [[NSMutableArray alloc] init];
for(NSString *_string in _StringAry)
{
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#"_0123456789"];
_string = [[_string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:#""];
[tmpAry addObject: [[_string copy] autorelease]];
}
NSLog(#"%#", tmpAry); // Gives the modified array
[tmpAry release];

How to Concatenate String in Objective-C (iPhone)? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I concatenate strings in Objective-C?
Firstly, the platform is iPhone and label.text changes the label displayed. Consider this scenario:
I've an array of integers. And I want to display it on the screen.
Here's my take on it:
-(IBAction) updateText: (id)sender {
int a[2];
a[0]=1;
a[1]=2;
a[2]=3;
for (int i=0; i<=10;i++)
label.text = [NSString stringByAppendingString: [NSString stringWithFormat: #"%i", a[i]]];
}
As you can probably see, I'm pretty confused. Pls pls help me out :(
Try this:
NSMutableString* theString = [NSMutableString string];
for (int i=0; i<=10;i++){
[theString appendString:[NSString stringWithFormat:#"%i ",i]];
}
label.text = theString;
Since you're using a loop, do be somewhat careful with both Tom and Benjie's solutions. They each create an extra autoreleased object per iteration. For a small loop, that's fine, but if the size of the loop is unbounded or if the strings are large, this can lead to a very large memory spike and performance hit. Particularly on iPhone, this is exactly the kind of loop that can lead to surprising memory problems due to short-lived memory spikes.
The following solution has a smaller memory footprint (it's also slightly faster and takes less typing). Note the call to -appendFormat: rather than -appendString. This avoids creating a second string that will be thrown away. Remember that the final string has an extra space at the end that you may want to get rid of. You can fix that by either treating the first or last iteration differently, or by trimming the last space after the loop.
NSMutableString* theString = [NSMutableString string];
for (int i=0; i<=10;i++){
[theString appendFormat:#"%i ",i];
}
label.text = theString;
Don't forget [NSArray componentsJoinedByString:]. In this case you don't have an NSArray, but in the common cases where you do, this is probably the best way to get what you're looking for.
//NSArray *chunks
string = [chunks componentsJoinedByString: #","];
Another method without using NSMutableString:
NSString* theString = #"";
for (int i=0; i<=10;i++){
theString = [theString stringByAppendingFormat:#"%i ",i];
}
label.text = theString;
Here's a full implementation (correcting your ranges):
-(IBAction) updateText: (id)sender {
int a[3];
a[0]=1;
a[1]=2;
a[2]=3;
NSString *str = #"";
for (int i=0; i<3;i++)
str = [str stringByAppendingFormat:#"%i ",i];
label.text = str;
}
You could also do it like this (e.g. if you wanted a comma separated list):
-(IBAction) updateText: (id)sender {
int a[3];
a[0]=1;
a[1]=2;
a[2]=3;
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:3];
for (int i=0; i<3;i++)
[arr addObject:[NSString stringWithFormat:#"%i",i]];
label.text = [arr componentsJoinedByString:#", "];
}