NSMutableString is null after appending - ios

I am trying to build an array of strings which will be added to an XML soap request.
I have a simple loop to build the array:
MarcTagsList is an NSArray of strings which contains the values ["82a","100a","245b","520a"] and is passed as a parameter.
NSMutableString *xmlTagList;
for(i=0; i<numberOfTags; i++)
{
[xmlTagList appendFormat:#"<string>%#</string>",MarcTagList[i]];
NSLog(#" appending - <string>%#</string>",MarcTagList[i]);
}
The log output shows the loop and MarcTag values are OK, however when the loop is complete
NSLog(#"xmlTagList %#", xmlTagList);
Shows xmlTagList to be null.

Firstly intialize xmlTagList string
//allocated
NSMutableString *xmlTagList=[[NSMutableString alloc] init];
OR
//auto-referenced
NSMutableString *xmlTagList=[NSMutableString string];
OR
//auto-referenced
NSMutableString *xmlTagList=[NSMutableString new];
OR
//auto-referenced
NSMutableString *xmlTagList= #"";
Now use xmlTagList for further requirement ie append operation
for(i=0; i<numberOfTags; i++)
{
[xmlTagList appendFormat:#"<string>%#</string>",MarcTagList[i]];
NSLog(#" appending - <string>%#</string>",MarcTagList[i]);
}

Change:
NSMutableString *xmlTagList;
to:
NSMutableString *xmlTagList = [NSMutableString stringWithString:#""];

Related

How can I change substring(s) in an array of strings in Objective-C?

I have a weird error with decoding & sign so its always shown as &, so && will be &&, a&b is shown as a&b etc.
I have those weird characters in my array of strings sometimes, so I want to remove them. To be more clear, if I have an array like this:
"str1", "a&b", "12345", "d&&emo&"
I want to it to be:
"str1", "a&b", "12345", "d&&emo&"
So all & should be changed into just &.
This is the code I tried, but it didn't help. Elements are not changing their values:
for (int i = 0; i <= self.array-1; i++)
{
NSLog(#"%#", self.array[i]);
[self.array[i] stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
NSLog(#"%#", self.array[i]);
}
I am new to Objective-C.
NSString instances are immutable, which means you can't change them once they are created. The stringByReplacingOccurencesOfString:withString: method actually returns a new NSString instance. You could use NSMutableString, or alternatively, you can create a new array containing the replacement strings, e.g.:
NSMutableArray *newArray = [NSMutableArray array];
for (NSString *input in self.array)
{
NSString *replacement = [input stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
[newArray addObject:replacement];
}
self.array = newArray;

How to add multiple strings in NSMutableString separated by comma

I want to create a comma separated string inside loop like this :-
string=#"A,B,C,D,E";
I did something like this :
for (int j=0; j<_arrayToCarryDataFromCartToPaymentPageInPaypal.count; j++)
{
NSMutableDictionary *paypalArrayDataInDictionary=[_arrayToCarryDataFromCartToPaymentPageInPaypal objectAtIndex:j];
[productNameDetail appendString:[NSString stringWithFormat:#"%#,", [paypalArrayDataInDictionary objectForKey:#"PRODUCT"]]];
}
Here _arrayToCarryDataFromCartToPaymentPageInPaypal is my MutableArray getting from plist.
Get array of PRODUCT from the _arrayToCarryDataFromCartToPaymentPageInPaypal and then use componentsJoinedByString on it to get the string separated by ,.
NSArray *products = [_arrayToCarryDataFromCartToPaymentPageInPaypal valueForKey:#"PRODUCT"];
NSString *str = [products componentsJoinedByString:#","];

how to get single characters from string in ios for Gujrati language(other language)

I am trying to get single characters from NSString, like "ઐતિહાસિક","પ્રકાશન","ક્રોધ". I want output like 1)ઐ,તિ,હા,સિ,ક 2) પ્ર,કા,શ,ન 3) ક્રો,ધ, but output is coming like this 1)ઐ , ત , િ , હ , િ , ક 2) પ , ્ , ર , ક , ા , શ , ન 3)ક , ્ , ર , ો , ધ
I have used code like below:
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i=0; i<strElement.length; i++)
{
NSString *str = [strElement substringWithRange:NSMakeRange(i, 1)];
[array addObject:str];
}
NSLog(#"%#",array);
Let's take strElement as "ક્રોધ" then I got output like this ક , ્ , ર , ો , ધ
But I need output like this ક્રો,ધ
Is there any way that I can get the desired output? Any method available directly in iOS or need to create it by my self then any way or idea how to create it?
Any help is appreciated
Your code is assuming that each character in the string is a single unichar value. But it is not. Some of the Unicode characters are composed of multiple unichar values.
The solution is to use rangeOfComposedCharacterSequenceAtIndex: instead of substringWithRange: with a fixed range length of 1.
NSString *strElement = #"ઐતિહાસિક પ્રકાશન ક્રોધ";
NSMutableArray *array = [[NSMutableArray alloc]init];
NSInteger i = 0;
while (i < strElement.length) {
NSRange range = [strElement rangeOfComposedCharacterSequenceAtIndex:i];
NSString *str = [strElement substringWithRange:range];
[array addObject:str];
i = range.location + range.length;
}
// Log the results. Build the results into a mutable string to avoid
// the ugly Unicode escapes shown by simply logging the array.
NSMutableString *res = [NSMutableString string];
for (NSString *str in array) {
if (res.length) {
[res appendString:#", "];
}
[res appendString:str];
}
NSLog(#"Results: %#", res);
This outputs:
Results: ઐ, તિ, હા, સિ, ક, , પ્ર, કા, શ, ન, , ક્રો, ધ

How to add values or add object into NSArray in Objective C using for loop?

Well this is my code:
NSDictionary *dicto;
NSString *ndccode;
NSString *string=#" ";
for (int i=0; i<array.count; i++) {
dicto=[array objectAtIndex:i];
ndccode=[dicto objectForKey:#"NDCCode"];
string=[string stringByAppendingString:ndccode];
string=[string stringByAppendingString:#"\n"];
NSLog(#"%#",string);
}
NSLog(#"%#",string);
In the above code, i have values in dicto nsdictionary which loops one by one value and assigns to ndccode which is string. Then I am adding to string so that I can append it to the next line.
output :
name1
name2
name3
.....
Instead of assigning to the string. I want to assign it to an array.Could you please tell me how to assign it to an array for this example,since it is in loop. I want to use the array for later purposes.
Thank you in advance.
NSDictionary *dicto;
NSString *ndccode;
NSMutableArray *outputArray=[NSMutableArray new];
for (int i=0; i<array.count; i++) {
dicto=[array objectAtIndex:i];
ndccode=[dicto objectForKey:#"NDCCode"];
[outputArray addObject:ndccode];
}
or even more succinctly
NSArray *outputArray = [array valueForKey:#"NDCCode"];

stringByAppendingString not concatenating

I'm trying to take an array of strings and take each item and put it into a string format. I wrote a method to do this as I need to concatenate the listed array values into another string. For some reason I cannot get the array values to list properly, a blank string is returned.
- (NSString*)listParameters:(NSArray*)params
{
NSString *outputList = #"";
if (params) {
for (int i=0; i<[params count]; i++) {
NSLog(#"%#",[params objectAtIndex:i]);
[outputList stringByAppendingString:[params objectAtIndex:i]];
if (i < ([params count] - 1)) {
[outputList stringByAppendingString:#", "];
}
}
}
NSLog(#"outputList: %#", outputList);
return outputList;
}
The first log statement properly returns a string (so there is definitely a string in the array), but the second log statement only returns "outputList: ".
I tried making outputList start as more than just an empty string which didn't work. I also tried assigning [params objectAtIndex:i] to a string then appending it, didn't work either.
I feel like I'm missing something obvious here, but I cannot get it to work.
How can I get this array of strings to print into a single string separated by commas?
You probably want to use a NSMutableString instead with its appendString method. NSString is immutable.
- (NSString*)listParameters:(NSArray*)params
{
NSMutableString *outputList = [[NSMutableString alloc] init];
if (params) {
for (int i=0; i<[params count]; i++) {
NSLog(#"%#",[params objectAtIndex:i]);
[outputList appendString:[params objectAtIndex:i]];
if (i < ([params count] - 1)) {
[outputList appendString:#", "];
}
}
}
NSLog(#"outputList: %#", outputList);
return outputList;
}
You need to assign the result of [outputList stringByAppendingString:[params objectAtIndex:i]] and [outputList stringByAppendingString:#", "] back to outputList.
It would be better still if you were using an instance of NSMutableString for outputList instead, as you're going to create a lot of autoreleased objects in that loop otherwise.
Try:
outputList = [outputList stringByAppendingString:#", "];
as stringByAppendingString works by returning a new String

Resources