Thursday, March 12, 2015 [Tweets] [Favorites]

Wrapping libxml2 for Swift

Janie Clayton:

I have included the profiler in my sample code for this project, but in case you don’t feel like running it yourself, my benchmarking showed that using libxml2 was four times faster than tree-based NSXMLDocument parsing and three times faster than event-based NSXMLDocument parsing.

[…]

I was becoming incredibly confused and frustrated when Brad had me add a println() in LibXMLDoc between when we initialize the document and when we initialize the root node. It turns out that ARC was deleting the LibXMLDoc immediately after it was being initialized because it wasn’t being held on to or referenced anywhere. D’oh!

[…]

The solution utilized here was to replace the strongly referenced LibXMLNode root node with a private, internal weak root node and a computed property checking to see if this internal root node has been set yet. If it has, it is returned. If it hans’t, we extract the root node, set it to the internal root node, and return it.

[…]

And this, kids, is why you still need to think about memory management and ARC even if you started coding after iOS 5, like I did.

Comments

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment