<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Saagar Jha</div>
</div>
<div><br class=""><blockquote type="cite" class=""><div class="">On Sep 5, 2017, at 01:19, Georgios Moschovitis via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Indeed it works, but I don’t get it.<div class="">What’s the difference?</div><div class=""><br class=""></div><div class="">-g.</div><div class=""><br class=""></div><div class="">PS: Btw, my original code was giving `seg-fault: 11` even on macOS.</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 5 Sep 2017, at 10:53 AM, CK TUNG via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">This revised code, as below, works without segmentation fault</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br data-mce-bogus="1" class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">import</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>Foundation</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"></span><br class=""></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">class</span><span class="s3" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>ParserDelegate:<span class="Apple-converted-space"> </span></span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">NSObject</span><span class="s3" style="font-variant-ligatures: no-common-ligatures;">,<span class="Apple-converted-space"> </span></span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">XMLParserDelegate</span><span class="s3" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>{</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> </span><br class="webkit-block-placeholder"></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span></span><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">func</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>startParsing(</span><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">_</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>xml:</span><span class="s4" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">String</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">) {</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span></span><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">let</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>data = xml.</span><span class="s5" style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);">data</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">(using: .</span><span class="s4" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">utf8</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">)!</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span></span><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">let</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>xmlParser =<span class="Apple-converted-space"> </span></span><span class="s4" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">XMLParser</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">(data: data)</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> xmlParser.</span><span class="s4" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">delegate</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>=<span class="Apple-converted-space"> </span></span><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">self</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> xmlParser.</span><span class="s5" style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);">parse</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">()</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> }</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> </span><br class="webkit-block-placeholder"></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span></span><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">func</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>parserDidStartDocument(</span><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">_</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>parser:<span class="Apple-converted-space"> </span></span><span class="s4" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">XMLParser</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">) {</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span class="s3" style="font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span></span><span class="s5" style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);">print</span><span class="s3" style="font-variant-ligatures: no-common-ligatures;">(</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">"Starting document"</span><span class="s3" style="font-variant-ligatures: no-common-ligatures;">)</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> }</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> </span><br class="webkit-block-placeholder"></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span></span><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">func</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>parser(</span><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">_</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>parser:<span class="Apple-converted-space"> </span></span><span class="s4" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">XMLParser</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">, didStartElement elementName:<span class="Apple-converted-space"> </span></span><span class="s4" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">String</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">, namespaceURI:<span class="Apple-converted-space"> </span></span><span class="s4" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">String</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">?, qualifiedName qName:<span class="Apple-converted-space"> </span></span><span class="s4" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">String</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">?, attributes attributeDict: [</span><span class="s4" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">String</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>:<span class="Apple-converted-space"> </span></span><span class="s4" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">String</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">]) {</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span></span><span class="s5" style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);">print</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">(</span><span class="s6" style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"***<span class="Apple-converted-space"> </span></span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">\</span><span class="s6" style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">(</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">elementName</span><span class="s6" style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">)"</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">)</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"> }</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;">}</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="s2" style="font-variant-ligatures: no-common-ligatures;"></span><br class=""></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">let</span><span class="s3" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>xml =<span class="Apple-converted-space"> </span></span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">"<root><title name=\"test\">George</title></root>"</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span class="s1" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">let</span><span class="s3" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>test =<span class="Apple-converted-space"> </span></span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">ParserDelegate</span><span class="s3" style="font-variant-ligatures: no-common-ligatures;">()</span></div><div style="margin: 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span class="s7" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">test</span><span class="s3" style="font-variant-ligatures: no-common-ligatures;">.</span><span class="s2" style="font-variant-ligatures: no-common-ligatures;">startParsing</span><span class="s3" style="font-variant-ligatures: no-common-ligatures;">(</span><span class="s7" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">xml</span><span class="s3" style="font-variant-ligatures: no-common-ligatures;">)</span></div></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class="">On Sep 05, 2017, at 02:24 PM, Georgios Moschovitis via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class="msg-quote"><div class="_stretch"><span class="body-text-content">As an example, this SegFaults:<br class=""><br class="">import Foundation<br class=""><br class="">class ParserDelegate: NSObject, XMLParserDelegate {<br class="">func parserDidStartDocument(_ parser: XMLParser) {<br class="">print("Starting document")<br class="">}<br class=""><br class="">func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {<br class="">print("*** \(elementName)")<br class="">}<br class="">}<br class=""><br class="">let xml = "<root><title name=\"test\">George</title></root>"<br class="">let data = xml.data(using: .utf8)!<br class="">let xmlParser = XMLParser(data: data)<br class="">xmlParser.delegate = ParserDelegate()<br class=""></span></div></div></blockquote></div></div></blockquote></div></div></div></div></blockquote><div><br class=""></div><div>XMLParser’s delegate is unowned, so it’s being deallocated when you exit the current scope. Hold on to it with a strong reference:</div><div><br class=""></div><div>let delegate = ParserDelegate()</div><div>xmlParser.delegate = delegate</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class="msg-quote"><div class="_stretch"><span class="body-text-content">xmlParser.parse()<br class=""><br class=""><blockquote type="cite" class="quoted-plain-text">On 5 Sep 2017, at 9:01 AM, Georgios Moschovitis <<a href="mailto:george.moschovitis@icloud.com" class="">george.moschovitis@icloud.com</a>> wrote:</blockquote><blockquote type="cite" class="quoted-plain-text"><br class=""></blockquote><blockquote type="cite" class="quoted-plain-text">Hi,</blockquote><blockquote type="cite" class="quoted-plain-text"><br class=""></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 class=""></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 class=""></blockquote><blockquote type="cite" class="quoted-plain-text">Any suggestions?</blockquote><blockquote type="cite" class="quoted-plain-text"><br class=""></blockquote><blockquote type="cite" class="quoted-plain-text">-g.</blockquote><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" data-mce-href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></span></div></div></blockquote></div><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-users mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-users@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">swift-users@swift.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></div></blockquote></div><br class=""></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>