NSMutableString append is not working [duplicate] - iphone

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:#""];

Related

how to convert Nsstring value into Hexdecimal in ios [duplicate]

This question already has answers here:
How to convert an NSString to hex values
(8 answers)
Closed 9 years ago.
Hello i'm beginner in iOS In one of my activity I have NSString and want to convert into Hexdecimal format
NSString *str= 131003112444371;
long long decimalRepresentation = 131003112444371;
NSLog(#"date %llx",decimalRepresentation);
NSLog(#"Hex value of char is 0x%02llx",decimalRepresentation);
I am using this I then get result 0x772589fb51d3 and I want to extract this no 772589fb51d3
When I am using these line for this ....
NSString *str1=[NSString stringWithFormat:#"%llu",decimalRepresentation];
NSLog(#"str %#",str1);
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"0x"];
str1 = [[str1 componentsSeparatedByCharactersInSet:doNotWant]componentsJoinedByString:#""];
NSLog(#"%#", str1); // => foobarbazfoo
but this line again convert this hexdecimal value into string I don't extract this value
0x772589fb51d3 as 772589fb51d3 .please help me....
Thanks In advance
I have tried like this:-
NsString *str= 131003112444371;
long long decimalRepresentation = 131003112444371;
NSLog(#"date %llx",decimalRepresentation);
NSLog(#"Hex value of char is 0x%02llx",decimalRepresentation);
//just replaced llu to llx
NSString *str1=[NSString stringWithFormat:#"%llx",decimalRepresentation];
NSLog(#"str %#",str1);
And the output i got here is 772589fb51d3
One Possible solution of this question:
+(NSString*)hexFromStr:(NSString*)str
{
NSData* nsData = [str dataUsingEncoding:NSUTF8StringEncoding];
const char* data = [nsData bytes];
NSUInteger len = nsData.length;
NSMutableString* hex = [NSMutableString string];
for(int i = 0; i < len; ++i)[hex appendFormat:#"%02X", data[i]];
return hex;
}

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;);

NSMutableArray to byte array to string

I have an iPad app which communicates with a webservice. There I can download an encrypted file. In a particular request I get a json with login credentials. Also in that json is a key which is used to encrypt the data.
The key looks like:
[0,44,215,1,215,88,94,150]
With the json framework I can put this key into an NSMutableArray. After that I use a AES256 code to decrypt the file. But that code needs a NSString as a key. So my question is: how can I decode that NSMutableArray into an NSString? I guess I first need to put it into an byte arary, and then put it into an NSString?
Who can help me with this one?
Thanks in advance!
Firstly, convert your array of numbers (I assume they're given as NSNumbers) into a C array using code similar to the first snippet in the accepted answer here. In other words, something similar to this:
// Test array for now -- this data will come from JSON response
NSArray* nsArray = [NSArray arrayWithObjects:[NSNumber numberWithChar:1],
[NSNumber numberWithChar:2],
nil];
char cArray[2];
// Fill C-array with ints
int count = [nsArray count];
for (int i = 0; i < count; ++i) {
cArray[i] = [[nsArray objectAtIndex:i] charValue];
}
Then create an NSString using the correct encoding:
NSString *encodedStr = [NSString stringWithCString:cArray encoding:NSUTF8StringEncoding];
Note: these are code sketches, they haven't been tested!
EDIT: changed from ints to chars.
If your array is the sequence of numbers, you can loop through it
//Assume you have created keyArray from your JSON
NSMutableString * keyString = [NSMutableString string];
for (id element in keyArray) {
[string appendFormat:#"%#", id];
}
// if you need the comma's in the string
NSMutableString * keyString = [NSMutableString string];
for (id element in keyArray) {
[string appendFormat:#"%#,", id];
}
int length = [string length];
NSRange range = NSMakeRange(0, length-1);
string = [string substringWithRange:range];

store each letter of NSString as object of NSMutableArray

I have an NSString of integer values. I need to add each one of the integers as a separate object in an NSMutableArray.
I tried characterAtIndex: but I keep getting errors…
P.s. I've solved over 30 problems thank's to stackoverflow's search, but didn't find any information on this problem.
NSMutableArray *results = [NSMutableArray array];
for (int i = 0; i < [string length]; i++)
{
NSString *substr = [string substringWithRange:NSMakeRange(i,1)];
[results addObject:[NSNumber numberWithInt:[substr intValue]];
}
NSLog(#" %# separated into: %#", string, results);
Consider looking at componentsSeparatedByString: or componentsSeparatedByCharactersInSet: for your purpose. Both methods are available on NSString.
If your integers are more than one digit or are space-separated, consider alternately using an NSScanner - you can read instances of NSInteger off the string one by one, wrap them in NSNumbers, and stick them in your array.

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:#", "];
}