Skip to content

Commit ff496c7

Browse files
committed
update for Swift 1.2, Xcode 6.3
1 parent 724bc3d commit ff496c7

File tree

37 files changed

+805
-161
lines changed

37 files changed

+805
-161
lines changed

bk2ch10p495TableWithDynamicType/TableWithDynamicType/MasterViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class MasterViewController: UITableViewController {
3232
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
3333
if segue.identifier == "showDetail" {
3434
let indexPath = self.tableView.indexPathForSelectedRow()!
35-
let object = objects[indexPath.row] as NSDate
36-
(segue.destinationViewController as DetailViewController).detailItem = object
35+
let object = objects[indexPath.row] as! NSDate
36+
(segue.destinationViewController as! DetailViewController).detailItem = object
3737
}
3838
}
3939

@@ -58,9 +58,9 @@ class MasterViewController: UITableViewController {
5858
*/
5959

6060
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
61-
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
61+
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
6262

63-
let object = objects[indexPath.row] as NSDate
63+
let object = objects[indexPath.row] as! NSDate
6464
cell.textLabel!.text = object.description
6565
return cell
6666
}

bk2ch10p495dynamicType/ch23p670font1dynamicType/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class ViewController : UIViewController {
1616
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doDynamicType:", name: UIContentSizeCategoryDidChangeNotification, object: nil)
1717

1818
// UIFont.familyNames()
19-
// .map{UIFont.fontNamesForFamilyName($0 as String)}.map(println)
19+
// .map{UIFont.fontNamesForFamilyName($0 as! String)}.map(println)
2020

2121
}
2222

2323
func doDynamicType(n:NSNotification) {
24-
let style = self.lab.font.fontDescriptor().objectForKey(UIFontDescriptorTextStyleAttribute) as String
24+
let style = self.lab.font.fontDescriptor().objectForKey(UIFontDescriptorTextStyleAttribute) as! String
2525
self.lab.font = UIFont.preferredFontForTextStyle(style)
2626
}
2727

bk2ch10p498fontDescriptor/ch23p670font1dynamicType/ViewController.swift

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,38 @@ class ViewController : UIViewController {
1515
func doDynamicType(n:NSNotification!) {
1616
var fbody : UIFont!
1717
var femphasis : UIFont!
18-
let which = 3
18+
let which = 1
1919
switch which {
2020
case 1:
2121
let body = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFontTextStyleBody)
22-
let emphasis = body.fontDescriptorWithSymbolicTraits(.TraitItalic)
23-
fbody = UIFont(descriptor: body, size: 0)
24-
femphasis = UIFont(descriptor: emphasis, size: 0)
22+
if let emphasis = body.fontDescriptorWithSymbolicTraits(.TraitItalic) {
23+
fbody = UIFont(descriptor: body, size: 0)
24+
femphasis = UIFont(descriptor: emphasis, size: 0)
25+
}
2526
case 2:
26-
// this should work but doesn't (bug?)
27-
fbody = UIFont(name: "GillSans", size: 15)
28-
let emphasis = fbody.fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitItalic)
29-
femphasis = UIFont(descriptor: emphasis, size: 0)
27+
// this should work but doesn't (bug?), and we crash later
28+
// whoa, in iOS 8.3 it works!
29+
if let body = UIFont(name: "GillSans", size: 15),
30+
emphasis = body.fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitItalic) {
31+
fbody = body
32+
femphasis = UIFont(descriptor: emphasis, size: 0)
33+
}
3034
case 3:
3135
// the workaround is drop down to Core Text
3236
// unfortunately Swift seems unaware that CTFont and UIFont are now bridged
33-
fbody = UIFont(name: "GillSans", size: 15)
34-
let result = CTFontCreateCopyWithSymbolicTraits(fbody as AnyObject as CTFont, 0, nil, .ItalicTrait, .ItalicTrait)
35-
femphasis = result as AnyObject as UIFont
37+
// whoa, in Swift 1.2 it has heard about this!
38+
if let body = UIFont(name: "GillSans", size: 15),
39+
result = CTFontCreateCopyWithSymbolicTraits(body as CTFont, 0, nil, .ItalicTrait, .ItalicTrait) {
40+
fbody = body
41+
femphasis = result as UIFont
42+
}
3643
default:break
3744
}
3845

39-
let s = self.lab.text! as NSString
46+
let s = self.lab.text!
4047
let mas = NSMutableAttributedString(string: s, attributes: [NSFontAttributeName:fbody])
41-
mas.addAttribute(NSFontAttributeName, value: femphasis, range: s.rangeOfString("wild"))
48+
mas.addAttribute(NSFontAttributeName, value: femphasis, range: (s as NSString).rangeOfString("wild"))
4249
self.lab.attributedText = mas
4350
}
44-
51+
4552
}

bk2ch10p499fontDescriptor2/ch23p670font1dynamicType/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import UIKit
3-
import CoreText
3+
// import CoreText // no longer necessary to import core text
44

55
class ViewController : UIViewController {
66

bk2ch10p503attributedString/ch23p771attributedStringInLabel/ViewController.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ViewController : UIViewController {
2626
var content : NSMutableAttributedString!
2727
var content2 : NSMutableAttributedString!
2828

29-
let which = 5 // 0 ... 5
29+
let which = 0 // 0 ... 5
3030
switch which {
3131
case 0, 1, 4, 5:
3232
let s1 = "The Gettysburg Address, as delivered on a certain occasion " +
@@ -70,14 +70,15 @@ class ViewController : UIViewController {
7070
NSFontAttributeName: UIFont(name:"HoeflerText-Black", size:24)!,
7171
NSExpansionAttributeName: 0.3,
7272
NSKernAttributeName: -4 // negative kerning bug fixed in iOS 8
73+
// but they broke it again in iOS 8.3!
7374
], range:NSMakeRange(0,1))
7475
self.lab.attributedText = content2
7576
self.tv.attributedText = content2
7677
self.tv.contentInset = UIEdgeInsetsMake(20,0,0,0)
7778
if which > 2 {fallthrough}
7879
case 3, 4, 5:
7980
content2.addAttribute(NSParagraphStyleAttributeName,
80-
value:lend(){
81+
value:lend {
8182
(para:NSMutableParagraphStyle) in
8283
para.headIndent = 10
8384
para.firstLineHeadIndent = 10
@@ -106,9 +107,9 @@ class ViewController : UIViewController {
106107
inRange:NSMakeRange(0,content.length),
107108
options:opts,
108109
usingBlock: {
109-
(value:AnyObject!, range:NSRange, stop:UnsafeMutablePointer<ObjCBool>) -> Void in
110+
value, range, stop in
110111
println(range)
111-
let font = value as UIFont
112+
let font = value as! UIFont
112113
if font.pointSize == 15 {
113114
content.addAttribute(NSFontAttributeName,
114115
value:UIFont(name: "Arial-BoldMT", size:20)!,

bk2ch10p507tabStops/TextTabTest/ViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ViewController : UIViewController {
1818
let s = "Onions\t$2.34\nPeppers\t$15.2\n"
1919
let mas = NSMutableAttributedString(string:s, attributes:[
2020
NSFontAttributeName:UIFont(name:"GillSans", size:15)!,
21-
NSParagraphStyleAttributeName:lend() {
21+
NSParagraphStyleAttributeName:lend {
2222
(p:NSMutableParagraphStyle) in
2323
var tabs = [NSTextTab]()
2424
let terms = NSTextTab.columnTerminatorsForLocale(NSLocale.currentLocale())
@@ -78,10 +78,10 @@ class ViewController : UIViewController {
7878
let src = CGImageSourceCreateWithURL(url, nil)
7979
let scale = UIScreen.mainScreen().scale
8080
let w : CGFloat = 20 * scale
81-
let d : [String:AnyObject] = [
82-
kCGImageSourceShouldAllowFloat : kCFBooleanTrue,
83-
kCGImageSourceCreateThumbnailWithTransform: kCFBooleanTrue,
84-
kCGImageSourceCreateThumbnailFromImageAlways: kCFBooleanTrue,
81+
let d : [NSObject:AnyObject] = [
82+
kCGImageSourceShouldAllowFloat : true,
83+
kCGImageSourceCreateThumbnailWithTransform: true,
84+
kCGImageSourceCreateThumbnailFromImageAlways: true,
8585
kCGImageSourceThumbnailMaxPixelSize: Int(w)
8686
]
8787
let imref =

bk2ch10p508AttributedTextAsSecretMarking/AttributedTextAsSecretMarking/ViewController.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ class ViewController: UIViewController {
66
@IBOutlet var lab : UILabel!
77

88
@IBAction func doUpdateLabel(sender:AnyObject?) {
9-
let mas = self.lab.attributedText.mutableCopy() as NSMutableAttributedString
9+
let mas = self.lab.attributedText.mutableCopy() as! NSMutableAttributedString
1010
let r = (mas.string as NSString).rangeOfString("^0")
1111
if r.length > 0 {
1212
mas.addAttribute("HERE", value: 1, range: r)
1313
mas.replaceCharactersInRange(r, withString: NSDate().description)
1414
} else {
1515
mas.enumerateAttribute("HERE", inRange: NSMakeRange(0, mas.length), options: nil) {
1616
value, r, stop in
17-
if let value = value as? Int {
18-
if value == 1 {
19-
mas.replaceCharactersInRange(r, withString: NSDate().description)
20-
stop.memory = true
21-
}
17+
if let value = value as? Int where value == 1 {
18+
mas.replaceCharactersInRange(r, withString: NSDate().description)
19+
stop.memory = true
2220
}
2321
}
2422
}

bk2ch10p509attributedStringDrawing/ch23p773attributedStringDrawing/Images.xcassets/AppIcon.appiconset/Contents.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"idiom" : "iphone",
1515
"size" : "60x60",
1616
"scale" : "2x"
17+
},
18+
{
19+
"idiom" : "iphone",
20+
"size" : "60x60",
21+
"scale" : "3x"
1722
}
1823
],
1924
"info" : {

bk2ch10p509attributedStringDrawing/ch23p773attributedStringDrawing/StringDrawer.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@ import UIKit
44

55
class StringDrawer : UIView {
66
@NSCopying var attributedText : NSAttributedString! {
7-
didSet {
8-
self.setNeedsDisplay()
9-
}
7+
didSet {
8+
self.setNeedsDisplay()
9+
}
1010
}
1111

1212
override func drawRect(rect: CGRect) {
1313
let r = rect.rectByOffsetting(dx: 0, dy: 2)
14-
// unfortunately there's a huge bug in Swift:
15-
// we can't "or" NSStringDrawingOptions values together
16-
// I've resorted to the assistance of Objective-C
17-
// let opts = NSString.combine(.TruncatesLastVisibleLine, with:.UsesLineFragmentOrigin)
18-
// let opts = NSStringDrawingOptions.UsesLineFragmentOrigin
19-
14+
// bug trying to "or" NSStringDrawingOptions values together...
2015
// fixed in Swift 1.2 / Xcode 6.3!
2116
let opts : NSStringDrawingOptions = .TruncatesLastVisibleLine | .UsesLineFragmentOrigin
2217

bk2ch10p510attributedStringDrawing2/ch23p773attributedStringDrawing/StringDrawer.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

bk2ch10p510attributedStringDrawing2/ch23p773attributedStringDrawing/StringDrawer.m

Lines changed: 0 additions & 55 deletions
This file was deleted.

bk2ch10p510attributedStringDrawing2/ch23p773attributedStringDrawing/StringDrawer.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import UIKit
44

55
class StringDrawer : UIView {
66
@NSCopying var attributedText : NSAttributedString! {
7-
didSet {
8-
self.setNeedsDisplay()
9-
}
7+
didSet {
8+
self.setNeedsDisplay()
9+
}
1010
}
1111

1212
override func drawRect(rect: CGRect) {
@@ -15,13 +15,7 @@ class StringDrawer : UIView {
1515
switch which {
1616
case 1:
1717
let r = rect.rectByOffsetting(dx: 0, dy: 2)
18-
// unfortunately there's a huge bug in Swift:
19-
// we can't "or" NSStringDrawingOptions values together
20-
// you can say this:
21-
// let options : NSStringDrawingOptions = NSStringDrawingOptions(rawValue: NSStringDrawingOptions.TruncatesLastVisibleLine.rawValue | NSStringDrawingOptions.UsesLineFragmentOrigin.rawValue)!
22-
// but it crashes at runtime as being nil
23-
// I've resorted to the assistance of Objective-C
24-
let options = NSString.combine(.TruncatesLastVisibleLine, with:.UsesLineFragmentOrigin)
18+
let options : NSStringDrawingOptions = .TruncatesLastVisibleLine | .UsesLineFragmentOrigin
2519
self.attributedText.drawWithRect(r, options: options, context: nil)
2620
case 2:
2721
let lm = NSLayoutManager()

bk2ch10p510attributedStringDrawing2/ch23p773attributedStringDrawing/ch23p815attributedStringDrawing2-Bridging-Header.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
// Use this file to import your target's public headers that you would like to expose to Swift.
33
//
44

5-
#import "StringDrawer.h"

bk2ch10p510attributedStringDrawing2/ch23p815attributedStringDrawing2.xcodeproj/project.pbxproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
C9A50D79180F835200F5B6CE /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C9A50D78180F835200F5B6CE /* AppDelegate.m */; };
1818
C9A50D7C180F835200F5B6CE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C9A50D7A180F835200F5B6CE /* Main.storyboard */; };
1919
C9A50D81180F835300F5B6CE /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C9A50D80180F835300F5B6CE /* Images.xcassets */; };
20-
C9A50D9F1810085000F5B6CE /* StringDrawer.m in Sources */ = {isa = PBXBuildFile; fileRef = C9A50D9E1810085000F5B6CE /* StringDrawer.m */; };
2120
/* End PBXBuildFile section */
2221

2322
/* Begin PBXFileReference section */
@@ -37,8 +36,6 @@
3736
C9A50D7B180F835200F5B6CE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
3837
C9A50D80180F835300F5B6CE /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
3938
C9A50D87180F835300F5B6CE /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
40-
C9A50D9D1810085000F5B6CE /* StringDrawer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringDrawer.h; sourceTree = "<group>"; };
41-
C9A50D9E1810085000F5B6CE /* StringDrawer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StringDrawer.m; sourceTree = "<group>"; };
4239
/* End PBXFileReference section */
4340

4441
/* Begin PBXFrameworksBuildPhase section */
@@ -90,8 +87,6 @@
9087
C9A50D78180F835200F5B6CE /* AppDelegate.m */,
9188
C9A50D7A180F835200F5B6CE /* Main.storyboard */,
9289
32C95B71198BC18100708AC3 /* ViewController.swift */,
93-
C9A50D9D1810085000F5B6CE /* StringDrawer.h */,
94-
C9A50D9E1810085000F5B6CE /* StringDrawer.m */,
9590
32C95B73198BC1E700708AC3 /* StringDrawer.swift */,
9691
C9A50D80180F835300F5B6CE /* Images.xcassets */,
9792
C9A50D6F180F835200F5B6CE /* Supporting Files */,
@@ -177,7 +172,6 @@
177172
isa = PBXSourcesBuildPhase;
178173
buildActionMask = 2147483647;
179174
files = (
180-
C9A50D9F1810085000F5B6CE /* StringDrawer.m in Sources */,
181175
32C95B74198BC1E700708AC3 /* StringDrawer.swift in Sources */,
182176
C9A50D79180F835200F5B6CE /* AppDelegate.m in Sources */,
183177
32C95B72198BC18100708AC3 /* ViewController.swift in Sources */,

bk2ch10p512labelBreaking/LabelFontShrinkTest/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ class ViewController : UIViewController {
6161

6262
let mas = NSMutableAttributedString(string:s, attributes:[
6363
NSFontAttributeName:f,
64-
NSParagraphStyleAttributeName:lend({
64+
NSParagraphStyleAttributeName: lend {
6565
(para : NSMutableParagraphStyle) in
6666
para.alignment = align
6767
para.lineBreakMode = brk
68-
})
68+
}
6969
])
7070
mas.addAttribute(NSForegroundColorAttributeName,
7171
value:UIColor.blueColor(),

0 commit comments

Comments
 (0)