iOS: Display NSArray of NSStrings in a label with special character encoding - ios

I have several NSStrings which I add to an NSArray. The string may contain special characters. In the end I want to print the array to a UILabel.
The very simplified code (if you think I missed something, let me know):
NSString *strBuffer = #"Röckdöts";
NSLog(#"String: %#", strBuffer);
NSMutableArray *marrSelectedStrings = [[NSMutableArray alloc] init];
[marrSelectedStrings addObject:strBuffer];
NSLog(#"Array: %#", marrSelectedStrings);
NSUInteger iCount = [marrSelectedStrings count]
for (NSUInteger i = 0; i < iCount; i++)
{
NSLog(#"element %d: %#", i, [marrSelectedStrings objectAtIndex: i]);
}
In a different UIViewController:
self.label.text = [NSString stringWithFormat:#"%#", marrSelectedStrings];
The string itself prints out fine. For the array however, it depends on the output method, whether the console diplays the correct special character or code for it. The label only prints code instead of the real characters. The print out via NSLog looks like the following:
Buffer: Röckdöts
Array: (
R\U00f6ckd\U00f6ts
)
element 0: Röckdöts
Whereas the label reads:
R\U00f6ckd\U00f6ts
I tried using stringWithUTF8String during the adding to the array as well as encoding during assigning it to the label like so, but it didn't change the result:
// adding with UTF8 encoding
[marrSelectedStrings addObject:[NSString stringWithUTF8String:[strBuffer UTF8String]]];
// printing to label with UTF8 encoding
self.label.text = [NSString stringWithUTF8String:[[NSString stringWithFormat:#"%#", marrSelectedStrings] UTF8String]];
Is there an easier way to simply print the array with correct character encoding to the UILabel than iterating over the array and appending every single word?

Try this
NSString * result = [[marrSelectedStrings valueForKey:#"description"] componentsJoinedByString:#""];
self.label.text = result;

try like this
NSMutableArray *marrSelectedStrings = [[NSMutableArray alloc] init];
[marrSelectedStrings addObject:strBuffer];
NSString *description = [marrSelectedStrings description];
if (description) {
const char *descriptionChar = [description cStringUsingEncoding:NSASCIIStringEncoding];
if (descriptionChar) {
NSString *prettyDescription = [NSString stringWithCString:descriptionChar encoding:NSNonLossyASCIIStringEncoding];
if (prettyDescription) {
description = prettyDescription;
}
}
}
NSLog(#"Array: %#", description);

Related

Listing floats from array with format

I have an NSMutableArray that contains floats. I can display one float in a specific format or display all of them without formatting but I cannot figure out how to do both at once. I first wrap the float in an NSNumber so that it can be added to the array:
NSMutableArray *array = [[NSMutableArray alloc] init];
NSNumber *num = [NSNumber numberWithFloat:5.10f];
[array addObject:num];
NSNumberFormatter works for formatting the numbers to be displayed in a textView. Here is the code I used to do that:
for (NSNumber *aNumber in array) {
self.textView.text = [NSString stringWithFormat:#"%#"
[NSNumberFormatter localizedStringFromNumber:aNumber
numberStyle:NSNumberFormatterCurrencyStyle]];
}
The obvious problem here is the text is replaced with the number in the array each time the loop runs rather than listing all of them.
So, I did more research and found componentsJoinedByString:
self.textView.text = [NSString stringWithFormat:#"%#", [array componentsJoinedByString:#", "]];
This worked well to list them but lacked the formatting style: It displays, for example, 5.10 as 5.1 which I don't want. I tried to combine them somehow but it didn't work, and I also tried stringByAppendingString in the loop using the formatter but that was a mess that did not work at all.
You are replacing the text in the textView instead of appending..
for (NSNumber *aNumber in array) {
NSString *result = [NSNumberFormatter localizedStringFromNumber:aNumber
numberStyle:NSNumberFormatterCurrencyStyle];
if (aNumber == array.firstObject && !self.textView.text.length) {
self.textView.text = result;
}
else {
self.textView.text = [NSString stringWithFormat:#"%#, %#", self.textView.text, result];
}
}

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: ઐ, તિ, હા, સિ, ક, , પ્ર, કા, શ, ન, , ક્રો, ધ

present NSString as a line of elements

I have an NSString that hold data (actually that could be presented an NSArray). and i want to output that on a label.
In NSLog my NSString output is:
(
"cristian_camino",
"daddu_02",
"_ukendt_babe_",
"imurtaza.zoeb"
)
What i want is, to present it like :"cristian_camino","daddu_02","_ukendt_babe_","imurtaza.zoeb"
In a single line.
I could accomplish that turning string to an array and do following: arrayObjectAtIndex.0, arrayObjectAtIndex.1, arrayObjectAtIndex.2, arrayObjectAtIndex.3.
But thats look not good, and that objects may be nil, so i prefer NSString to hold data.
So, how could i write it in a single lane?
UPDATE:
There is the method i want to use to set text for UILabel:
-(void)setLikeLabelText:(UILabel*)label{
//Likes
NSString* likersCount = [self.photosDictionary valueForKeyPath:#"likes.count"];
NSString* likersRecent = [self.photosDictionary valueForKeyPath:#"likes.data.username"];
NSString *textString = [NSString stringWithFormat:#"%# - amount of people like it, recent "likes": %#", likersCount, likersRecent];
label.text = textString;
NSLog(#"text String is %#", textString);
}
valueForKeyPath: returns an NSArray, not an NSString. Whilst you've declared likersCount and likersRecent as instances of NSString, they're actually both arrays of values. You should be able to do something like the following to construct a string:
NSArray* likersRecent = [self.photosDictionary valueForKeyPath:#"likes.data.username"];
NSString *joined = [likersRecent componentsJoinedByString:#"\", \""];
NSString *result = [NSString stringWithFormat:#"\"%#\"", joined];
NSLog(#"Result: %#", result);
componentsJoinedByString: will join the elements of the array with ", ", and then the stringWithFormat call will add a " at the beginning and end.
The statement is incorrect, the internal quote marks (" that you want to display) need to be escaped:
NSString *textString = [NSString stringWithFormat:#"%# - amount of people like it, recent \"likes\": %#", likersCount, likersRecent];
If somebody curious how i fix it, there it is:
for (int i =0; i < [likersRecent count]; i++){
stringOfLikers = [stringOfLikers stringByAppendingString:[NSString stringWithFormat:#" %#", [likersRecent objectAtIndex:i]]];
}
Not using commas or dots though.

Replace String Between Two Strings

I have a serious problem about indexing in array. I've been working on this for 2 days and couldn't find answer yet.
I want to do that, search specific character in array then replace it with other string. I'm using replaceObjectAtIndex method but my code is doesn't work.
Here is my code;
NSString *commentText = commentTextView.text;
NSUInteger textLength = [commentText length];
NSString *atSign = #"#";
NSMutableArray *commentArray = [[NSMutableArray alloc] init];
[commentArray addObject:commentText];
for (int arrayCounter=1; arrayCounter<=textLength; arrayCounter++)
{
NSRange isRange = [commentText rangeOfString:atSign options:NSCaseInsensitiveSearch];
if(isRange.location != NSNotFound)
{
commentText = [commentText stringByReplacingOccurrencesOfString:commentText withString:atSign];
[_mentionsearch filtrele:_mentionText];
id<textSearchProtocol> delegate;
[delegate closeList:[[self.searchResult valueForKey:#"user_name"] objectAtIndex:indexPath.row]];
}
}
Ok, now i can find "#" sign in the text and i can match it. But this is the source of problem that, i can not replace any string with "#" sign. Here is the last part of code;
-(void)closeList
{
NSArray *arrayWithSign = [commentTextView.text componentsSeparatedByString:#" "];
NSMutableArray *arrayCopy = [arrayWithSign mutableCopy];
[arrayCopy replaceObjectAtIndex:isRange.location withObject:[NSString stringWithFormat:#"#%#",username]];
}
When im logging isRange.location value, it returns correct. But when im try to run, my application is crashing. So, i can not replacing [NSString stringWithFormat:#"#%#",username] parameter. How can i solve this problem?
If I understand correctly you want to change a substring in a string with a new string. In this case, why don't you use directly the stringByReplacingOccurrencesOfString method of NSString:
NSString *stringToBeChanged = #"...";
NSString *stringToBeChangedWith = #"...";
NSString *commentTextNew = [commentText stringByReplacingOccurrencesOfString:stringToBeChanged withString:stringToBeChangedWith];

How can I show all of objects in NSMutable Array on text field?

I want to show all objects in mutable array on to textfield, label, something else except NSLog
- (IBAction)purchasePressed:(id)sender {
NSMutableArray *addItem = [[NSMutableArray alloc] init];
[addItem addObject:#"Almond"];
[addItem addObject:#"Choc"];
"number" is my label (I'm not sure that all of Objects in MutableArray can be showed on textfield or not?) i can do it only with NSLog.
for (i = 0;i < [addItem count] ; i++ )
{
NSLog(#"%#", addItem);
NSString *test1=(#"%#", addItem);
number.text=test1;
}
Every time you set the text of a label you replace the previous text.
Try replacing your whole loop with something like:
number.text = [addItem componentsJoinedByString:#", "];
Which will create a single string from all of the strings in the array and add that to the label. You could do something similar in your loop if you want to.
If you want a string with all the values concatenated:
NSString *mainString = [NSString alloc] init];
for (NSString *item in addItem) {
mainString = [mainString stringByAppendingString:item];
}
number.text = mainString;
EDIT: Using NSMutableString
NSMutableString *mainString = [[NSMutableString alloc] init];
for (NSString *item in addItem) {
[mainString appendString:item];
}
number.text = mainString;

Resources