<html><body><div>This revised code, as below, works without segmentation fault</div><div><br data-mce-bogus="1"></div><div>




<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #31595d}
span.s1 {font-variant-ligatures: no-common-ligatures; color: #ba2da2}
span.s2 {font-variant-ligatures: no-common-ligatures}
span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}
span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}
span.s5 {font-variant-ligatures: no-common-ligatures; color: #3e1e81}
span.s6 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}
span.s7 {font-variant-ligatures: no-common-ligatures; color: #4f8187}
</style>


<p class="p1"><span class="s1">import</span><span class="s2"> Foundation</span></p>
<p class="p2"><span class="s2"></span><br></p>
<p class="p3"><span class="s1">class</span><span class="s3"> ParserDelegate: </span><span class="s2">NSObject</span><span class="s3">, </span><span class="s2">XMLParserDelegate</span><span class="s3"> {</span></p>
<p class="p2"><span class="s2">&nbsp;&nbsp; &nbsp;</span></p>
<p class="p1"><span class="s2">&nbsp; &nbsp; </span><span class="s1">func</span><span class="s2"> startParsing(</span><span class="s1">_</span><span class="s2"> xml:</span><span class="s4">String</span><span class="s2">) {</span></p>
<p class="p1"><span class="s2">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="s1">let</span><span class="s2"> data = xml.</span><span class="s5">data</span><span class="s2">(using: .</span><span class="s4">utf8</span><span class="s2">)!</span></p>
<p class="p1"><span class="s2">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="s1">let</span><span class="s2"> xmlParser = </span><span class="s4">XMLParser</span><span class="s2">(data: data)</span></p>
<p class="p1"><span class="s2">&nbsp; &nbsp; &nbsp; &nbsp; xmlParser.</span><span class="s4">delegate</span><span class="s2"> = </span><span class="s1">self</span></p>
<p class="p1"><span class="s2">&nbsp; &nbsp; &nbsp; &nbsp; xmlParser.</span><span class="s5">parse</span><span class="s2">()</span></p>
<p class="p1"><span class="s2">&nbsp; &nbsp; }</span></p>
<p class="p2"><span class="s2">&nbsp;&nbsp; &nbsp;</span></p>
<p class="p1"><span class="s2">&nbsp; &nbsp; </span><span class="s1">func</span><span class="s2"> parserDidStartDocument(</span><span class="s1">_</span><span class="s2"> parser: </span><span class="s4">XMLParser</span><span class="s2">) {</span></p>
<p class="p4"><span class="s3">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="s5">print</span><span class="s3">(</span><span class="s2">"Starting document"</span><span class="s3">)</span></p>
<p class="p1"><span class="s2">&nbsp; &nbsp; }</span></p>
<p class="p2"><span class="s2">&nbsp;&nbsp; &nbsp;</span></p>
<p class="p1"><span class="s2">&nbsp; &nbsp; </span><span class="s1">func</span><span class="s2"> parser(</span><span class="s1">_</span><span class="s2"> parser: </span><span class="s4">XMLParser</span><span class="s2">, didStartElement elementName: </span><span class="s4">String</span><span class="s2">, namespaceURI: </span><span class="s4">String</span><span class="s2">?, qualifiedName qName: </span><span class="s4">String</span><span class="s2">?, attributes attributeDict: [</span><span class="s4">String</span><span class="s2"> : </span><span class="s4">String</span><span class="s2">]) {</span></p>
<p class="p1"><span class="s2">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="s5">print</span><span class="s2">(</span><span class="s6">"*** </span><span class="s2">\</span><span class="s6">(</span><span class="s2">elementName</span><span class="s6">)"</span><span class="s2">)</span></p>
<p class="p1"><span class="s2">&nbsp; &nbsp; }</span></p>
<p class="p1"><span class="s2">}</span></p>
<p class="p2"><span class="s2"></span><br></p>
<p class="p4"><span class="s1">let</span><span class="s3"> xml = </span><span class="s2">"&lt;root&gt;&lt;title name=\"test\"&gt;George&lt;/title&gt;&lt;/root&gt;"</span></p>
<p class="p5"><span class="s1">let</span><span class="s3"> test = </span><span class="s2">ParserDelegate</span><span class="s3">()</span></p>
<p class="p6"><span class="s7">test</span><span class="s3">.</span><span class="s2">startParsing</span><span class="s3">(</span><span class="s7">xml</span><span class="s3">)</span></p></div><div><br>On Sep 05, 2017, at 02:24 PM, Georgios Moschovitis via swift-users &lt;swift-users@swift.org&gt; wrote:<br><br></div><div><blockquote type="cite"><div class="msg-quote"><div class="_stretch"><span class="body-text-content">As an example, this SegFaults:<br><br>import Foundation<br><br>class ParserDelegate: NSObject, XMLParserDelegate {<br> func parserDidStartDocument(_ parser: XMLParser) {<br> print("Starting document")<br> }<br><br> func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {<br> print("*** \(elementName)")<br> }<br>}<br><br>let xml = "&lt;root&gt;&lt;title name=\"test\"&gt;George&lt;/title&gt;&lt;/root&gt;"<br>let data = xml.data(using: .utf8)!<br>let xmlParser = XMLParser(data: data)<br>xmlParser.delegate = ParserDelegate()<br>xmlParser.parse()<br><br><blockquote type="cite" class="quoted-plain-text">On 5 Sep 2017, at 9:01 AM, Georgios Moschovitis &lt;george.moschovitis@icloud.com&gt; wrote:</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Hi,</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">I would like to parse an RSS feed using Swift 3.1 on Linux.</blockquote><blockquote type="cite" class="quoted-plain-text">I tried to use Foundations’s XML but I only managed to get segmentation faults.</blockquote><blockquote type="cite" class="quoted-plain-text">Is this supposed to work on Linux? I have only seen examples on iOS.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Apart from that a quick search didn’t reveal any useful XML parsing library compatible with Linux.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Any suggestions?</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">-g.</blockquote><br>_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" data-mce-href="mailto:swift-users@swift.org">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/mailman/listinfo/swift-users</a><br></span></div></div></blockquote></div></body></html>