XML parsing on Linux, with Swift 3.1

This revised code, as below, works without segmentation fault

import Foundation

class ParserDelegate: NSObject, XMLParserDelegate {

func startParsing\(\_ xml:String\) \{
    let data = xml\.data\(using: \.utf8\)\!
    let xmlParser = XMLParser\(data: data\)
    xmlParser\.delegate = self
    xmlParser\.parse\(\)
\}

func parserDidStartDocument\(\_ parser: XMLParser\) \{
    print\("Starting document"\)
\}

func parser\(\_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: \[String : String\]\) \{
    print\("\*\*\* \\\(elementName\)"\)
\}

}

let xml = "<root><title name=\"test\">George</title></root>"
let test = ParserDelegate()
test.startParsing(xml)

···

On Sep 05, 2017, at 02:24 PM, Georgios Moschovitis via swift-users <swift-users@swift.org> wrote:

As an example, this SegFaults:

import Foundation

class ParserDelegate: NSObject, XMLParserDelegate {
func parserDidStartDocument(_ parser: XMLParser) {
print("Starting document")
}

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
print("*** \(elementName)")
}

let xml = "<root><title name=\"test\">George</title></root>"
let data = xml.data(using: .utf8)!
let xmlParser = XMLParser(data: data)
xmlParser.delegate = ParserDelegate()
xmlParser.parse()

On 5 Sep 2017, at 9:01 AM, Georgios Moschovitis <george.moschovitis@icloud.com> wrote:

Hi,

I would like to parse an RSS feed using Swift 3.1 on Linux.
I tried to use Foundations’s XML but I only managed to get segmentation faults.
Is this supposed to work on Linux? I have only seen examples on iOS.

Apart from that a quick search didn’t reveal any useful XML parsing library compatible with Linux.

Any suggestions?

-g.

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Indeed it works, but I don’t get it.
What’s the difference?

-g.

PS: Btw, my original code was giving `seg-fault: 11` even on macOS.

···

On 5 Sep 2017, at 10:53 AM, CK TUNG via swift-users <swift-users@swift.org> wrote:

This revised code, as below, works without segmentation fault

import Foundation

class ParserDelegate: NSObject, XMLParserDelegate {
    
    func startParsing(_ xml:String) {
        let data = xml.data(using: .utf8)!
        let xmlParser = XMLParser(data: data)
        xmlParser.delegate = self
        xmlParser.parse()
    }
    
    func parserDidStartDocument(_ parser: XMLParser) {
        print("Starting document")
    }
    
    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
        print("*** \(elementName)")
    }
}

let xml = "<root><title name=\"test\">George</title></root>"
let test = ParserDelegate()
test.startParsing(xml)

On Sep 05, 2017, at 02:24 PM, Georgios Moschovitis via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

As an example, this SegFaults:

import Foundation

class ParserDelegate: NSObject, XMLParserDelegate {
func parserDidStartDocument(_ parser: XMLParser) {
print("Starting document")
}

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
print("*** \(elementName)")
}
}

let xml = "<root><title name=\"test\">George</title></root>"
let data = xml.data(using: .utf8)!
let xmlParser = XMLParser(data: data)
xmlParser.delegate = ParserDelegate()
xmlParser.parse()

On 5 Sep 2017, at 9:01 AM, Georgios Moschovitis <george.moschovitis@icloud.com <mailto:george.moschovitis@icloud.com>> wrote:

Hi,

I would like to parse an RSS feed using Swift 3.1 on Linux.
I tried to use Foundations’s XML but I only managed to get segmentation faults.
Is this supposed to work on Linux? I have only seen examples on iOS.

Apart from that a quick search didn’t reveal any useful XML parsing library compatible with Linux.

Any suggestions?

-g.

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

Saagar Jha

Indeed it works, but I don’t get it.
What’s the difference?

-g.

PS: Btw, my original code was giving `seg-fault: 11` even on macOS.

This revised code, as below, works without segmentation fault

import Foundation

class ParserDelegate: NSObject, XMLParserDelegate {
    
    func startParsing(_ xml:String) {
        let data = xml.data(using: .utf8)!
        let xmlParser = XMLParser(data: data)
        xmlParser.delegate = self
        xmlParser.parse()
    }
    
    func parserDidStartDocument(_ parser: XMLParser) {
        print("Starting document")
    }
    
    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
        print("*** \(elementName)")
    }
}

let xml = "<root><title name=\"test\">George</title></root>"
let test = ParserDelegate()
test.startParsing(xml)

As an example, this SegFaults:

import Foundation

class ParserDelegate: NSObject, XMLParserDelegate {
func parserDidStartDocument(_ parser: XMLParser) {
print("Starting document")
}

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
print("*** \(elementName)")
}
}

let xml = "<root><title name=\"test\">George</title></root>"
let data = xml.data(using: .utf8)!
let xmlParser = XMLParser(data: data)
xmlParser.delegate = ParserDelegate()

XMLParser’s delegate is unowned, so it’s being deallocated when you exit the current scope. Hold on to it with a strong reference:

let delegate = ParserDelegate()
xmlParser.delegate = delegate

···

On Sep 5, 2017, at 01:19, Georgios Moschovitis via swift-users <swift-users@swift.org> wrote:

On 5 Sep 2017, at 10:53 AM, CK TUNG via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
On Sep 05, 2017, at 02:24 PM, Georgios Moschovitis via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

xmlParser.parse()

On 5 Sep 2017, at 9:01 AM, Georgios Moschovitis <george.moschovitis@icloud.com <mailto:george.moschovitis@icloud.com>> wrote:

Hi,

I would like to parse an RSS feed using Swift 3.1 on Linux.
I tried to use Foundations’s XML but I only managed to get segmentation faults.
Is this supposed to work on Linux? I have only seen examples on iOS.

Apart from that a quick search didn’t reveal any useful XML parsing library compatible with Linux.

Any suggestions?

-g.

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

XMLParser’s delegate is unowned, so it’s being deallocated when you exit the current scope. Hold on to it with a strong reference:

let delegate = ParserDelegate()
xmlParser.delegate = delegate

Wow!
Need to brush-up my understanding of Swift ‘pointers’

Thank you.

Quick question (for my own edification, as well as to demonstrate to my students the power of this mail list :wink:), is CustomStringConvertible a stored property or a computed property? The docs say the latter, but when using Xcode’s fabulous FIX button, it seems to imply it’s a stored property.

Michael

It cannot be ‘stored’ I would think.
I always compute my implementations.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - An HTTP(S) web server framework in Swift

···

On 08 Sep 2017, at 19:47, Michael Rogers via swift-users <swift-users@swift.org> wrote:

Quick question (for my own edification, as well as to demonstrate to my students the power of this mail list :wink:), is CustomStringConvertible a stored property or a computed property? The docs say the latter, but when using Xcode’s fabulous FIX button, it seems to imply it’s a stored property.

Michael
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Thanks for your reply, Rien. I just tried this again, and now it seems that either is acceptable. Seems a little weird, but I guess I’d rather have more flexibility than less.

protocol Powerable {
    var isOn:Bool { get set } // Must have get and set
    var usesDC:Bool { get } // Must have get, may have set
    func turnOn()
    func turnOff()
}

class GPS : Powerable, CustomStringConvertible {
    // Either of these work
// var description: String {
// return "I am a GPS"
// }
    var description:String = "I am a GPS"
    var isOn: Bool = false
    var usesDC: Bool = false
    
    // does the obvious
    func turnOn(){
        isOn = true
    }
    
    // does the obvious
    func turnOff() {
        isOn = false
    }
}

print(GPS()) // output: I am a GPS (both cases)

Michael

···

On Sep 8, 2017, at 1:21 PM, Rien <Rien@Balancingrock.nl> wrote:

It cannot be ‘stored’ I would think.
I always compute my implementations.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - An HTTP(S) web server framework in Swift

On 08 Sep 2017, at 19:47, Michael Rogers via swift-users <swift-users@swift.org> wrote:

Quick question (for my own edification, as well as to demonstrate to my students the power of this mail list :wink:), is CustomStringConvertible a stored property or a computed property? The docs say the latter, but when using Xcode’s fabulous FIX button, it seems to imply it’s a stored property.

Michael
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Ah, yes. It is possible to ‘store’ it as a fixed string.
But I would still expect the compiler to treat it as if it were a computed property.
Given that this is a protocol and a protocol cannot define stored properties.
(yet anyway)

OTOH, what do I know about compilers… lol!

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - An HTTP(S) web server framework in Swift

···

On 08 Sep 2017, at 20:26, Michael Rogers <mprogers@mac.com> wrote:

Thanks for your reply, Rien. I just tried this again, and now it seems that either is acceptable. Seems a little weird, but I guess I’d rather have more flexibility than less.

protocol Powerable {
    var isOn:Bool { get set } // Must have get and set
    var usesDC:Bool { get } // Must have get, may have set
    func turnOn()
    func turnOff()
}

class GPS : Powerable, CustomStringConvertible {
    // Either of these work
// var description: String {
// return "I am a GPS"
// }
    var description:String = "I am a GPS"
    var isOn: Bool = false
    var usesDC: Bool = false
    
    // does the obvious
    func turnOn(){
        isOn = true
    }
    
    // does the obvious
    func turnOff() {
        isOn = false
    }
}

print(GPS()) // output: I am a GPS (both cases)

Michael

On Sep 8, 2017, at 1:21 PM, Rien <Rien@Balancingrock.nl> wrote:

It cannot be ‘stored’ I would think.
I always compute my implementations.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - An HTTP(S) web server framework in Swift

On 08 Sep 2017, at 19:47, Michael Rogers via swift-users <swift-users@swift.org> wrote:

Quick question (for my own edification, as well as to demonstrate to my students the power of this mail list :wink:), is CustomStringConvertible a stored property or a computed property? The docs say the latter, but when using Xcode’s fabulous FIX button, it seems to imply it’s a stored property.

Michael
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Greetings

Thanks for your reply, Rien. I just tried this again, and now it seems that either is acceptable. Seems a little weird, but I guess I’d rather have more flexibility than less.

class GPS : Powerable, CustomStringConvertible {
    // Either of these work
// var description: String {
// return "I am a GPS"
// }
    var description:String = "I am a GPS"

You can also implement description as a let, since there is no intention to modify it :

class GPS : Powerable, CustomStringConvertible
{
  let description = "I am a GPS"
  
  …
}

Joanna

···

--
Joanna Carter
Carter Consulting