Skip to content

Commit 45b8c72

Browse files
committed
update for Swift 1.2, Xcode 6.3
1 parent 4955f9c commit 45b8c72

File tree

23 files changed

+134
-109
lines changed

23 files changed

+134
-109
lines changed

bk2ch23p798basicFileOperations/ch36p1053basicFileOperations/Person.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Person: NSObject, NSCoding {
2323
}
2424

2525
required init(coder: NSCoder) {
26-
self.lastName = coder.decodeObjectForKey("last")! as String
27-
self.firstName = coder.decodeObjectForKey("first")! as String
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
}

bk2ch23p798basicFileOperations/ch36p1053basicFileOperations/ViewController.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ViewController: UIViewController {
1313
}
1414

1515
@IBAction func doButton1 (sender:AnyObject!) {
16-
let docs = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last as String
16+
let docs = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last as! String
1717
println(docs)
1818
}
1919

@@ -72,7 +72,7 @@ class ViewController: UIViewController {
7272
println(err)
7373
return
7474
}
75-
(arr! as [NSURL]).map{$0.lastPathComponent}.map(println)
75+
(arr as! [NSURL]).map{$0.lastPathComponent!}.map(println)
7676
// ======
7777
// self.query.enumerateResultsUsingBlock {
7878
// obj, ix, stop in
@@ -88,19 +88,20 @@ class ViewController: UIViewController {
8888
// this is what you do when an Objective-C enumerable doesn't conform to Swift's SequenceType
8989
while let f = dir.nextObject() as? NSURL {
9090
if f.pathExtension == "txt" {
91-
println(f.lastPathComponent)
91+
println(f.lastPathComponent!)
9292
}
9393
}
9494
}
9595

96+
let which = 1
97+
9698
@IBAction func doButton8 (sender:AnyObject!) {
9799
let fm = NSFileManager()
98100
var err : NSError?
99101
let docsurl = fm.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true, error: &err)!
100102
let moi = Person(firstName: "Matt", lastName: "Neuburg")
101103
let moidata = NSKeyedArchiver.archivedDataWithRootObject(moi)
102104
let moifile = docsurl.URLByAppendingPathComponent("moi.txt")
103-
let which = 1
104105
switch which {
105106
case 1:
106107
moidata.writeToURL(moifile, atomically: true)
@@ -123,11 +124,10 @@ class ViewController: UIViewController {
123124
var err : NSError?
124125
let docsurl = fm.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true, error: &err)!
125126
let moifile = docsurl.URLByAppendingPathComponent("moi.txt")
126-
let which = 1
127127
switch which {
128128
case 1:
129129
let persondata = NSData(contentsOfURL: moifile)!
130-
let person = NSKeyedUnarchiver.unarchiveObjectWithData(persondata) as Person
130+
let person = NSKeyedUnarchiver.unarchiveObjectWithData(persondata) as! Person
131131
println(person)
132132
case 2:
133133
// ==== the NSFileCoordinator way
@@ -136,7 +136,7 @@ class ViewController: UIViewController {
136136
fc.coordinateAccessWithIntents([intent], queue: NSOperationQueue.mainQueue()) {
137137
(err:NSError!) in
138138
let persondata = NSData(contentsOfURL: intent.URL)!
139-
let person = NSKeyedUnarchiver.unarchiveObjectWithData(persondata) as Person
139+
let person = NSKeyedUnarchiver.unarchiveObjectWithData(persondata) as! Person
140140
println(person)
141141
}
142142
default:break

bk2ch23p802fileHandoff/ch36p1060fileHandoff/AppDelegate.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class AppDelegate : UIResponder, UIApplicationDelegate {
1818
println("start \(__FUNCTION__)")
1919
println(url)
2020

21+
22+
var finalurl = url
23+
2124
// new code: copy out of the inbox into the documents folder
2225
// I don't know why, but for some reason Quick Look can no longer preview
2326
// a document in the inbox
24-
25-
var finalurl = url
27+
2628
let dir = url.URLByDeletingLastPathComponent?.lastPathComponent
2729
if dir == "Inbox" {
2830
println("inbox")
@@ -44,7 +46,7 @@ class AppDelegate : UIResponder, UIApplicationDelegate {
4446
}
4547
}
4648

47-
let vc = self.window!.rootViewController as ViewController
49+
let vc = self.window!.rootViewController as! ViewController
4850
vc.displayDoc(finalurl)
4951
println("end \(__FUNCTION__)")
5052
return true

bk2ch23p802fileHandoff/ch36p1060fileHandoff/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class ViewController: UIViewController, UIDocumentInteractionControllerDelegate,
5555
return
5656
}
5757
self.dic.URL = url
58-
let v = sender as UIView
58+
let v = sender as! UIView
5959
let ok = self.dic.presentOpenInMenuFromRect(v.bounds, inView: v, animated: true)
6060
if !ok {
6161
println("That didn't work out")

bk2ch23p809peopleGroups/ch36p1066peopleGroups/DocumentLister.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class DocumentLister: UITableViewController {
88
var docsurl : NSURL {
99
get {
1010
let del = UIApplication.sharedApplication().delegate
11-
if let ubiq = (del as AppDelegate).ubiq {
11+
if let ubiq = (del as! AppDelegate).ubiq {
1212
return ubiq
1313
} else {
1414
let fm = NSFileManager()
@@ -34,7 +34,7 @@ class DocumentLister: UITableViewController {
3434
self.files = fm.contentsOfDirectoryAtURL(
3535
self.docsurl, includingPropertiesForKeys: nil, options: nil, error: nil)!
3636
.filter
37-
{ ($0 as NSURL).pathExtension == "pplgrp" } as [NSURL]
37+
{ ($0 as! NSURL).pathExtension == "pplgrp" } as! [NSURL]
3838
self.tableView.reloadData()
3939
}
4040

@@ -47,7 +47,7 @@ class DocumentLister: UITableViewController {
4747
av.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
4848
av.addAction(UIAlertAction(title: "OK", style: .Default) {
4949
_ in
50-
let name = (av.textFields![0] as UITextField).text
50+
let name = (av.textFields![0] as! UITextField).text
5151
if name == nil || name == "" {return}
5252
// why on earth is this an optional?? seems like a bug to me
5353
let url = self.docsurl.URLByAppendingPathComponent(name.stringByAppendingPathExtension("pplgrp")!)
@@ -72,7 +72,7 @@ class DocumentLister: UITableViewController {
7272
}
7373

7474
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
75-
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as UITableViewCell
75+
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as! UITableViewCell
7676
let fileURL = self.files[indexPath.row]
7777
cell.textLabel!.text = fileURL.lastPathComponent!.stringByDeletingPathExtension
7878
cell.accessoryType = .DisclosureIndicator

bk2ch23p809peopleGroups/ch36p1066peopleGroups/PeopleDocument.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class PeopleDocument: UIDocument {
1111
error outError: NSErrorPointer) -> Bool {
1212
println("loading \(typeName)")
1313
if let contents = contents as? NSData {
14-
let arr = NSKeyedUnarchiver.unarchiveObjectWithData(contents) as [Person]
14+
let arr = NSKeyedUnarchiver.unarchiveObjectWithData(contents) as! [Person]
1515
self.people = arr
1616
return true
1717
}

bk2ch23p809peopleGroups/ch36p1066peopleGroups/PeopleLister.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class PeopleLister: UITableViewController, UITextFieldDelegate {
6060
self.tableView.reloadData()
6161
self.tableView.scrollToRowAtIndexPath(ix, atScrollPosition:.Bottom, animated:true)
6262
let cell = self.tableView.cellForRowAtIndexPath(ix)!
63-
let tf = cell.viewWithTag(1) as UITextField
63+
let tf = cell.viewWithTag(1) as! UITextField
6464
tf.becomeFirstResponder()
6565

6666
self.doc.updateChangeCount(.Done)
@@ -78,9 +78,9 @@ class PeopleLister: UITableViewController, UITextFieldDelegate {
7878
}
7979

8080
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
81-
let cell = tableView.dequeueReusableCellWithIdentifier("Person", forIndexPath:indexPath) as UITableViewCell
82-
let first = cell.viewWithTag(1) as UITextField
83-
let last = cell.viewWithTag(2) as UITextField
81+
let cell = tableView.dequeueReusableCellWithIdentifier("Person", forIndexPath:indexPath) as! UITableViewCell
82+
let first = cell.viewWithTag(1) as! UITextField
83+
let last = cell.viewWithTag(2) as! UITextField
8484
let p = self.people[indexPath.row]
8585
first.text = p.firstName
8686
last.text = p.lastName
@@ -93,7 +93,7 @@ class PeopleLister: UITableViewController, UITextFieldDelegate {
9393
println("did end editing")
9494
var v = textField.superview!
9595
while !(v is UITableViewCell) {v = v.superview!}
96-
let cell = v as UITableViewCell
96+
let cell = v as! UITableViewCell
9797
let ip = self.tableView.indexPathForCell(cell)!
9898
let row = ip.row
9999
let p = self.people[row]

bk2ch23p809peopleGroups/ch36p1066peopleGroups/Person.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Person: NSObject, NSCoding {
2323
}
2424

2525
required init(coder: NSCoder) {
26-
self.lastName = coder.decodeObjectForKey("last")! as String
27-
self.firstName = coder.decodeObjectForKey("first")! as String
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/MyPeopleParser.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ class MyPeopleParser : MyXMLParserDelegate {
66

77
var people = [Person]()
88

9-
func parser(parser: NSXMLParser, didStartElement elementName: String!,
10-
namespaceURI: String!, qualifiedName qName: String!,
9+
func parser(parser: NSXMLParser, didStartElement elementName: String,
10+
namespaceURI: String?, qualifiedName qName: String?,
1111
attributes attributeDict: [NSObject : AnyObject]!) {
1212
if elementName == "person" {
1313
self.makeChild(MyPersonParser.self, elementName: elementName, parser: parser)
1414
}
1515
}
1616
override func finishedChild(s: String) {
17-
self.people.append((self.child as MyPersonParser).person)
17+
self.people.append((self.child as! MyPersonParser).person)
1818
}
1919

2020
}

bk2ch23p818xml/ch36p1070xml/MyPersonParser.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class MyPersonParser : MyXMLParserDelegate {
66

77
var person = Person(firstName: "", lastName: "")
88

9-
func parser(parser: NSXMLParser, didStartElement elementName: String!,
10-
namespaceURI: String!, qualifiedName qName: String!,
9+
func parser(parser: NSXMLParser, didStartElement elementName: String,
10+
namespaceURI: String?, qualifiedName qName: String?,
1111
attributes attributeDict: [NSObject : AnyObject]!) {
1212
self.makeChild(MyXMLParserDelegate.self, elementName: elementName, parser: parser)
1313
}

bk2ch23p818xml/ch36p1070xml/MyXMLParserDelegate.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class MyXMLParserDelegate : NSObject {
2727
}
2828

2929
extension MyXMLParserDelegate : NSXMLParserDelegate {
30-
func parser(parser: NSXMLParser, foundCharacters string: String!) {
31-
self.text = self.text + string
30+
func parser(parser: NSXMLParser, foundCharacters string: String?) {
31+
self.text = self.text + string!
3232
}
3333

34-
func parser(parser: NSXMLParser, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {
34+
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
3535
if self.parent != nil {
3636
self.parent!.finishedChild(self.text)
3737
parser.delegate = self.parent

bk2ch23p818xml/ch36p1070xml/Person.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Person: NSObject, NSCoding {
2323
}
2424

2525
required init(coder: NSCoder) {
26-
self.lastName = coder.decodeObjectForKey("last")! as String
27-
self.firstName = coder.decodeObjectForKey("first")! as String
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
}

0 commit comments

Comments
 (0)