String literals not requiring special characters?

Is there a literal string in Objective-c that does not require escaping special characters? In other words, I'm looking for the equivalent of a triple quote from Python.

I am trying to put some HTML in NSString and would like to avoid having to avoid quotes from all HTML attributes.

+6
source share
4 answers

There is no triple quote equivalent; String literals should always use escape files for special characters.

Perhaps it is best to put your HTML in a file separately from your source, and then create a line with -[NSString initWithContentsOfFile:encoding:error:] (or the associated initWithContentsOfURL:... ).

+2
source

In C ++ 11, you can do this. See my answer to a similar question .

For this you need Objective-C ++ 11 in your case. It should work, although in gcc.

 const char * html = R"HTML( <HTML> <HEAD> <TITLE> [Python-Dev] Triple-quoted strings and indentation </TITLE> </HEAD> <BODY BGCOLOR="#ffffff"> blah blah blah </BODY> </HTML> )HTML"; int main() { } 

g ++ -std = C ++ 0x -o raw_string raw_string.mm is at least compiled.

+3
source

No, unfortunately, no ... there is some excellent information about string literals in obj-c here:
http://blog.ablepear.com/2010/07/objective-c-tuesdays-string-literals.html

+1
source

For those who are reading this now:

This function has been implemented since Swift 4. Read more here: https://github.com/apple/swift-evolution/blob/master/proposals/0168-multi-line-string-literals.md

You can do:

 let author = "Im the author!" let x = """ <?xml version="1.0"?> <catalog> <book id="bk101" empty=""> <author>\(author)</author> <title>XML Developer Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> </catalog> """ 
0
source

Source: https://habr.com/ru/post/906978/


All Articles