Skip to content

Commit 57ae769

Browse files
committed
update for 6.1 final, code cleanup (get rid of protocol and optional func)
1 parent c2dd967 commit 57ae769

File tree

5 files changed

+28
-31
lines changed

5 files changed

+28
-31
lines changed

bk2ch23p818xml/ch36p1070xml/MyPeopleParser.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ class MyPeopleParser : MyXMLParserDelegate {
1313
self.makeChild(MyPersonParser.self, elementName: elementName, parser: parser)
1414
}
1515
}
16-
17-
func finishedChild(s: String) {
16+
override func finishedChild(s: String) {
1817
self.people.append((self.child as MyPersonParser).person)
1918
}
2019

bk2ch23p818xml/ch36p1070xml/MyPersonParser.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ import UIKit
44

55
class MyPersonParser : MyXMLParserDelegate {
66

7-
var person : Person! = nil
7+
var person = Person(firstName: "", lastName: "")
88

99
func parser(parser: NSXMLParser, didStartElement elementName: String!,
1010
namespaceURI: String!, qualifiedName qName: String!,
1111
attributes attributeDict: [NSObject : AnyObject]!) {
1212
self.makeChild(MyXMLParserDelegate.self, elementName: elementName, parser: parser)
1313
}
1414

15-
func finishedChild(s: String) {
16-
if self.person == nil {
17-
self.person = Person(firstName: "", lastName: "")
18-
}
15+
override func finishedChild(s: String) {
1916
self.person.setValue(s, forKey:self.child.name)
2017
}
2118

bk2ch23p818xml/ch36p1070xml/MyXMLParserDelegate.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
import UIKit
44

5-
// protocol as a way of declaring an abstract method to be inherited and implemented by subclasses
6-
@objc protocol MyXMLParserDelegateProtocol {
7-
optional func finishedChild(s:String)
8-
}
95

10-
class MyXMLParserDelegate : NSObject, NSXMLParserDelegate, MyXMLParserDelegateProtocol {
11-
var text = ""
6+
class MyXMLParserDelegate : NSObject {
127
var name : String!
8+
var text = ""
139
weak var parent : MyXMLParserDelegate?
1410
var child : MyXMLParserDelegate!
1511

16-
required init(name:String!, parent:MyXMLParserDelegate!) {
12+
required init(name:String, parent:MyXMLParserDelegate?) {
1713
self.name = name
1814
self.parent = parent
1915
super.init()
@@ -24,21 +20,24 @@ class MyXMLParserDelegate : NSObject, NSXMLParserDelegate, MyXMLParserDelegatePr
2420
self.child = del
2521
parser.delegate = del
2622
}
23+
func finishedChild(s:String) {
24+
fatalError("Subclass must implement finishedChild:!")
25+
}
2726

28-
// func finishedChild(s:String) {} // subclass may implement as desired
29-
30-
// NSXMLParser delegate messages
31-
27+
}
28+
29+
extension MyXMLParserDelegate : NSXMLParserDelegate {
3230
func parser(parser: NSXMLParser, foundCharacters string: String!) {
3331
self.text = self.text + string
3432
}
3533

3634
func parser(parser: NSXMLParser, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {
3735
if self.parent != nil {
38-
// rather tricky syntax to allow calling optional method; bug in Swift?
39-
(self.parent! as MyXMLParserDelegateProtocol).finishedChild?(self.text)
36+
self.parent!.finishedChild(self.text)
4037
parser.delegate = self.parent
4138
}
4239
}
4340

4441
}
42+
43+

bk2ch23p818xml/ch36p1070xml/Person.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Person: NSObject, NSCoding {
66
@NSCopying var firstName : NSString
77
@NSCopying var lastName : NSString
88

9-
override var description : String! {
9+
override var description : String {
1010
return self.firstName + " " + self.lastName
1111
}
1212

@@ -23,8 +23,8 @@ class Person: NSObject, NSCoding {
2323
}
2424

2525
required init(coder: NSCoder) {
26-
self.lastName = coder.decodeObjectForKey("last")! as NSString
27-
self.firstName = coder.decodeObjectForKey("first")! as NSString
26+
self.lastName = coder.decodeObjectForKey("last")! as String
27+
self.firstName = coder.decodeObjectForKey("first")! as String
2828
// do not call super init(coder:) in this case
2929
super.init()
3030
}

bk2ch23p818xml/ch36p1070xml/ViewController.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ class ViewController: UIViewController {
66
// if this works, you'll see a list of people appear in the console
77

88
@IBAction func doButton (sender:AnyObject!) {
9-
let url = NSBundle.mainBundle().URLForResource("folks", withExtension: "xml")!
10-
let parser = NSXMLParser(contentsOfURL: url)
11-
let people = MyPeopleParser(name:"", parent:nil)
12-
parser.delegate = people
13-
parser.parse()
14-
15-
// ... done, do something with people.people ...
16-
println(people.people)
9+
if let url = NSBundle.mainBundle().URLForResource("folks", withExtension: "xml") {
10+
if let parser = NSXMLParser(contentsOfURL: url) {
11+
let people = MyPeopleParser(name:"", parent:nil)
12+
parser.delegate = people
13+
parser.parse()
14+
15+
// ... done, do something with people.people ...
16+
println(people.people)
17+
}
18+
}
1719
}
1820

1921

0 commit comments

Comments
 (0)