yahoojapan / SwiftyXMLParser

Simple XML Parser implemented in Swift
MIT License
578 stars 89 forks source link

Illegal instruction: 4 #50

Closed bigtrade2017 closed 2 years ago

bigtrade2017 commented 4 years ago

I'm using Xcode 11.5 & Swift 5.2.4. Trying to parse xml like:

let buffer = """
<_otherElement>…</_otherElement>
<_elementIneed>…</_elementIneed>
<_otherElement>…</_otherElement>
<_otherElement>…</_otherElement>
<_elementIneed>…</_elementIneed>
<_otherElement>…</_otherElement>
<_elementIneed>…</_elementIneed>
"""

I want to access sequence ("_elementIneed"), but project doesn't get compiled and Xcode suggests to report a bug.

var xml: XML.Accessor!
xml = XML.parse(buffer)
xml._elementIneed.makeIterator() //<---
xml._elementIneed.flatMap { $0 } //<---
xml._elementIneed.first //<---
xml._elementIneed.last //<---

xml._elementIneed.map { $0 } // ok
kazuhiro4949 commented 4 years ago

@bigtrade2017 Thank you for your reporting. I checked the code snippet on Xcode 11.5, Swift 5.2.4, and I didn't get the compiler error.

But I found some mistakes in your code. First, you need to add "do-try-catch" around XML.parse(:_). Second, XML has the root element. The document is illegal.

Could you check that?

bigtrade2017 commented 4 years ago

Hi, please try this one:

let buffer = """
<root>
<otherElement>…</otherElement>
<myElement><test1></test1></myElement>
<otherElement>…</otherElement>
<otherElement>…</otherElement>
<myElement><test2></test2></myElement>
<otherElement>…</otherElement>
<myElement><test3></test3></myElement>
</root>
""".data(using: .utf8)!

let xml = XML.parse(buffer)

//xml.root.myElement.flatMap { $0 } //<--- illegal instruction 4

//let map = xml.root.myElement.map { $0 } // ok

let filter = xml.root.element?.childElements.filter { $0.name == "myElement" }.forEach {
    print($0.name)
}// ok, but not so nice

//for element in xml.root.myElement {
//    print(element.name)
//} //<--- illegal instruction 4

//xml.root.myElement.forEach { element in
//    print(element.name)
//} //<--- illegal instruction 4
bigtrade2017 commented 4 years ago

And, maybe it's possible to add to existing data somehow?

For example, like this:

let buffer2 = buffer
let xml2 = XML.parse(buffer2)
let _xml = xml + xml2

//or like this:
xml.add(xml2)
bigtrade2017 commented 4 years ago

Hey, have you tried?:)

kazuhiro4949 commented 4 years ago

I'm sorry for being late. I'll try soom 🙌

kazuhiro4949 commented 3 years ago

I executed the code on Xcode12 and Swift5.3. It worked fine. Could you try on the environment?