How to create an XML literal string using Objective-C?

I have long XML strings that I hardcode into the unit tests of the iPhone project.

This is pretty ugly to avoid all quotes and line breaks - for example:

NSString *xml = 
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<root>\
    <element name=\"foo\" />\
</root>";

It would be nice to have a lower friction method.

I know that Ruby has great syntax for multi-line literals ... any suggestions for Objective-C?

+3
source share
1 answer

ObjC also has multi-line literals, and single quotes are legal in XML:

NSString *xml = 
@"<?xml version='1.0' encoding='UTF-8'?>"
 "<root>"
    "<element name='foo' />"
 "</root>";

Note that this does not insert a line into a line that is slightly different from your code.

+7
source

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


All Articles