Book Image

Swift Essentials - Second Edition

By : Alex Blewitt
Book Image

Swift Essentials - Second Edition

By: Alex Blewitt

Overview of this book

Swift was considered one of the biggest innovations last year, and certainly with Swift 2 announced at WWDC in 2015, this segment of the developer space will continue to be hot and dominating. This is a fast-paced guide to provide an overview of Swift programming and then walks you through in detail how to write iOS applications. Progress through chapters on custom views, networking, parsing and build a complete application as a Git repository, all by using Swift as the core language
Table of Contents (17 chapters)
Swift Essentials Second Edition
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Index

Parsing XML


Although JSON is more commonly used, there are still many XML-based network services. Fortunately XML parsing has existed in iOS since version 5 in the NSXMLParser class and is simple to access from Swift. For example, some data feeds (such as blog posts) use XML documents, such as Atom or RSS.

The NSXMLParser is a stream-oriented parser; that is, it reports individual elements as they are seen. The parser calls the delegate to notify when elements are seen and have finished. When an element is seen, the parser also includes any attributes that were present; and for text nodes, the string content. Parsing an XML file involves some state management in the parser. The example used in this section will be to parse an Atom (news feed) file, whose (simplified) structure looks like this:

<feed xmlns="http://www.w3.org/2005/Atom">
  <title>AlBlue's Blog</title>
  <link href="http://alblue.bandlem.com/atom.xml" rel="self"/>
  <entry>
    <title type="html...