Swift Difference Between Literal And String Newline Characters - swift

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.

Related

Text String Over-Bar not working with simple back-slash

I want to learn how to place and overbar on text string.
I tried the back slash on a text string and it did not work, does this trick only work on pin names?

String that contains English and Hebrew letters gets messed up after String.Join() - C# .NET

I have a string containing both English and Hebrew characters:
"Hitachi - היטצ'י:Hitachi – cartel CRT"
1st Step: flip the two parts that are separated by :.
Expected result: "Hitachi – cartel CRT:Hitachi - היטצ'י"
Next: I would like to concatenate the following text: ":אגם:עץ תיוק"
Final expected result: "Hitachi - cartel CRT:Hitachi - אגם:עץ תיוק:היטצ'י"
Actual result: "Hitachi – cartel CRT:Hitachi - היטצ'י:אגם:עץ תיוק"
This is my current code:
string path = "Hitachi - היטצ'י:Hitachi – cartel CRT";
string[] splittedByColonPath = path.Split(':');
Array.Reverse(splittedByColonPath);
List<string> list = new List<string>(splittedByColonPath);
list.Add("אגם:עץ תיוק:");
string result = String.Join(":", list.ToArray());
Any ideas on how to rearrange it the proper way?
The String.Join is working just fine, and the string is exactly what you want it to be. (You can test this if you like by writing some code to printthe string one character at a time, one character on each line.) The trouble is that, when displaying it, all the Hebrew text and colons is treated as one phrase, and since Hebrew is primarily right-to-left that means the first word in the phrase appears on the right.
Depending on what you want to achieve, this may be fine (eg if you're passing it to another program that expects data separeated by colons - in that case, the string may look wrong, but the other program will interpret it just fine). But if you want it to look how you're expecting, you have to force the display algorithm to treat the colons as left-to-right. You may be able to do this by changing the code to be
string result = String.Join("\u200e:"), list.ToArray());
The \u200e is a left-to-right marker (LRM), which causes any adjacent punctuation to be treated as left-to-right.
The downside of doing this is that any other program interpreting the data may not expect the LRM and may be confused by it.

Convert unicode to emoji

I'm looking for a way to represent an emoji 📄 in my code as unicode which is then displayed as an actual 'image' in output text. I'd like to use http://apps.timwhitlock.info/unicode/inspect/hex/1F4C4 to display the 'page facing up' in application, but I don't like the idea of having pictures in my code (though it is working fine) ;)
You can use arbitrary Unicode characters directly in your source code
let string = "📄"
or use the Swift Unicode escape sequence:
let string = "\u{1F4C4}"
More information in the section about "String Literals" in the Swift reference.

Allowed characters in CSS 'content' property?

I've read that we must use Unicode values inside the content CSS property i.e. \ followed by the special character's hexadecimal number.
But what characters, other than alphanumerics, are actually allowed to be placed as is in the value of content property? (Google has no clue, hence the question.)
The rules for “escaping” characters are in the CSS 2.1 specification, clause 4.1.3 Characters and case. The special rules for quoted strings, as in content property value, are in clause 4.3.7 Strings. Within a quoted string, any character may appear as such, except for the character used to quote the string (" or '), a newline character, or a backslash character \.
The information that you must use \ escapes is thus wrong. You may use them, and may even need to use them if the character encoding of the document containing the style sheet does not let you enter all characters directly. But if the encoding is UTF-8, and is properly declared, then you can write content: '☺ Я Ω ⁴ ®'.
As far as I know, you can insert any Unicode character. (Here's a useful list of Unicode characters and their codes.)
To utilize these codes, you must escape them, like so:
U+27BA Becomes \27BA
Or, alternatively, I think you may just be able to escape the character itself:
content: '\➺';
Source: http://mathiasbynens.be/notes/css-escapes

Formatting string from plist dictionary with new lines

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.