Formatting string from plist dictionary with new lines - iphone

I have some strings stored in a plist dictionary, the values of which i would like to display in a textview.
the strings are stored with \n for new lines, however when i pull out the string, it shows \n characters as literals instead of converting them to new lines
Here is the code to pull out the value
self.directions.text = [NSString stringWithFormat:#"%#",[self.exerciseDetail objectForKey:DIRECTIONS_KEY]];
How can i fix it?
Stored strings: some text \n some more text \n some more text

The problem here is that the plist file is storing the string with "\" and "n" characters, rather than a single newline character. The best fix (in my opinion) would be to fix the plist file to have the correct characters. If you are using the plist editor built into Xcode, then hitting the Enter key moves you to the next field. To get around this, hold the Option key while typing Enter and a newline will be inserted.

Related

Swift Difference Between Literal And String Newline Characters

I have a Swift application for iOS where inherit from XMLParser to parse an XML file. In one section I grab an attribute which may contain \n characters. These strings are used as button titles later.
Sample XML:
<button title="TitleLine1\nTitleLine2"/>
And in my didStartElement() function:
var buttonTitle : String = attributeDict["title"]
This grabs the title just as I would expect, however later when I create the button and apply the title, the newline character is simply printed right in line on the button instead of making the button title two lines.
I expect the button to look like this:
TitleLine1
TitleLine2
But it ends up being:
TitleLine1\nTitleLine2
If I forget the XML parsing altogether and simply hard code the button title with a literal string like this, the newline works:
button.setTitle("TitleLine1\nTitleLine2", for: .normal)
However setting it with my variable results in the newline being printed in line:
button.setTitle(buttonTitle, for: .normal)
Is there some different way the XMLParser handles strings? I notice the same behavior if I simply do a print() with the XML parsed string vs a literal string.
A \n in a Swift string literal is converted to an actual newline character during compilation.
The character \ followed by the character n in some piece of text obtained at runtime has no special meaning of any kind.
You have a few choices. When you parse the XML, you can process all strings and replace occurrences of the character sequence \n with an actual newline character:
let str = someXMLValue.replacingOccurrences(of: "\\n", with: "\n")
Another option is to change your XML structure to use an element for the title instead of an attribute. Then you can put a real newline in the title:
<button>
<title>TitleLine1
TitleLine2</title>
</button>
Then your Swift code doesn't need to do anything special to deal with newlines.

How to parse special characters in XML for iPad?

I am getting problem while parsing xml files that contains some special characters like single quote,double quote (', "")etc.I am using NSXMLParser's parser:foundCharacters:method to collect characters in my code.
<synctext type = "word" >They raced to the park Arthur pointed to a sign "Whats that say" he asked Zoo said DW Easy as pie</synctext>
When i parse and save the text from above tag of my xml file,the resultant string is appearing,in GDB, as
"\n\t\tThey raced to the park Arthur pointed to a sign \"Whats that say\" he asked Zoo said DW Easy as pie";
Observe there are 2 issues:
1)Unwanted characters at the beginning of the string.
2)The double quotes around Whats that say.
Can any one please help me how to get rid of these unwanted characters and how to read special characters properly.
NSString*string =[string stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" \n\t"]];
The parser is apparently returning exactly what's in the string. That is, the XML was coded with the starting tag on one line, a newline, two tabs, and the start of the string. And quotes in the string are obviously there in the original (and it's not clear in at least this example why you'd want to delete them).
But if you want these characters gone then you need to post-process the string. You can use Rams' statement to eliminate the newline and tabs, and stringByReplacingOccurrencesOfString:WithString: to zap the quotes.
(Note that some XML parsers can be instructed to return strings like this with the leading/trailing stuff stripped, but I'm not sure about this one. The quotes will always be there, though.)

while adding string(object) to the array giving unformatted string printing in nslog

I am trying to read Ò ÉÑÎÔÜ Ê characters from XML and want to store it in an array. While reading strings from XML it is working showing fine and they are the same as the XML characters.
Now I inserted them into a mutable array. When I am trying to print that array, it gives strange values: instead of ÒÙ ÉÑÎÔÜ Ê it shows "\U00dc","\U00ca\U00db\U00ce\U00cd", "\U00d7",(the array values).
Can anyone gives me some suggestion why it is happening?
I am using [textWord addObject:currentElement]; to insert objects to the array. Here currentElement is an NSMutableString.
these are the unicode character for you special character, whats the problem with that.

Emoji iCons are not displaying correctly when read from plist

I am trying to read some text from a plist file and display it to the users in alert box.
When I build the string using this code, everything works (users sees Hello with a smily icon):
NSString *hello = #"Hello \ue415";
but when I get the string from plist, using this code, uses sees "Hello \ue415":
NString *hello = (NSString *)[pageLiteratureDic objectForKey:litratureKey];
Do I have to encode string differently? Any help or pointers will be much appreciated... everyone love emojis ;)
You shouldn't literally type "\ue415" as text into the plist file. \u.... is an escape sequence in the syntax of strings and characters in the C language. The string itself does not contain backslash and "u" and whatever, it contains just 1 character, the Unicode character at the codepoint 0xe415. If you want to save that in a plist, you have to manually type that one Unicode character in there yourself, making sure to use whatever encoding that is required of a plist (maybe utf-8 or utf-16, not sure). Alternately, you can write a program that creates a plist from that string, and then copy and paste whatever is in that plist file over to your file.
In the plist, instead of "Hello \ue415" try using the smily face character explicitly as in "Hello :)". Just cut and paste the smily character over the unicode code. The reading of the plist is probably escaping the backslash and stopping the interpretation as a unicode character.

Plist contains the "&" character

I'm having a .plist file which has some values with the "&" sign, for example "M&I". When I save the file to the document folder and load it from there, I'm getting an empty dictionary. Any idea to how to fix this issue?
If you are directly modifying the XML file, you have to escape certain characters - & should be escaped using the XML entity &. If you use the editors, this should be done automatically for you.
If you use CDATA sections instead, you don't have to escape the characters.
If you insert the values when open plist file as property list it would do it automatically.