Skip to content

Commit 4066ea1

Browse files
wmcbainkevin-hirsch
authored andcommitted
Update code to Swift 3 version (AssistoLab#41)
* Update for Xcode 8 & Swift 3.0 * Address pull request concerns. * Fix indentation and update API.
1 parent 773c3e6 commit 4066ea1

13 files changed

+211
-197
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ DerivedData
3131
# Add this line if you want to avoid checking in source code from Carthage dependencies.
3232
# Carthage/Checkouts
3333

34-
Carthage/Build
34+
Carthage/Build
35+
36+
# Mac OS X
37+
.DS_Store
38+

Demo/AppDelegate.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1414

1515
var window: UIWindow?
1616

17-
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
17+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
1818
DropDown.startListeningToKeyboard()
1919

2020
return true
2121
}
2222

23-
}
23+
}

Demo/NiceButton.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,43 @@ class NiceButton: UIButton {
2121

2222
view.addConstraint(NSLayoutConstraint(
2323
item: view,
24-
attribute: .Height,
25-
relatedBy: .Equal,
24+
attribute: .height,
25+
relatedBy: .equal,
2626
toItem: nil,
27-
attribute: .Height,
27+
attribute: .height,
2828
multiplier: 1,
2929
constant: 1
3030
)
3131
)
3232

3333
addConstraint(NSLayoutConstraint(
3434
item: view,
35-
attribute: .Left,
36-
relatedBy: .Equal,
35+
attribute: .left,
36+
relatedBy: .equal,
3737
toItem: self,
38-
attribute: .Left,
38+
attribute: .left,
3939
multiplier: 1,
4040
constant: 0
4141
)
4242
)
4343

4444
addConstraint(NSLayoutConstraint(
4545
item: view,
46-
attribute: .Right,
47-
relatedBy: .Equal,
46+
attribute: .right,
47+
relatedBy: .equal,
4848
toItem: self,
49-
attribute: .Right,
49+
attribute: .right,
5050
multiplier: 1,
5151
constant: 0
5252
)
5353
)
5454

5555
addConstraint(NSLayoutConstraint(
5656
item: view,
57-
attribute: .Bottom,
58-
relatedBy: .Equal,
57+
attribute: .bottom,
58+
relatedBy: .equal,
5959
toItem: self,
60-
attribute: .Bottom,
60+
attribute: .bottom,
6161
multiplier: 1,
6262
constant: 0
6363
)

Demo/ViewController.swift

+23-23
Original file line numberDiff line numberDiff line change
@@ -40,69 +40,69 @@ class ViewController: UIViewController {
4040

4141
//MARK: - Actions
4242

43-
@IBAction func chooseArticle(sender: AnyObject) {
43+
@IBAction func chooseArticle(_ sender: AnyObject) {
4444
chooseArticleDropDown.show()
4545
}
4646

47-
@IBAction func changeAmount(sender: AnyObject) {
47+
@IBAction func changeAmount(_ sender: AnyObject) {
4848
amountDropDown.show()
4949
}
5050

51-
@IBAction func choose(sender: AnyObject) {
51+
@IBAction func choose(_ sender: AnyObject) {
5252
chooseDropDown.show()
5353
}
5454

55-
@IBAction func showCenteredDropDown(sender: AnyObject) {
55+
@IBAction func showCenteredDropDown(_ sender: AnyObject) {
5656
centeredDropDown.show()
5757
}
5858

59-
@IBAction func showBarButtonDropDown(sender: AnyObject) {
59+
@IBAction func showBarButtonDropDown(_ sender: AnyObject) {
6060
rightBarDropDown.show()
6161
}
6262

63-
@IBAction func changeDIsmissMode(sender: UISegmentedControl) {
63+
@IBAction func changeDIsmissMode(_ sender: UISegmentedControl) {
6464
switch sender.selectedSegmentIndex {
65-
case 0: dropDowns.forEach { $0.dismissMode = .Automatic }
66-
case 1: dropDowns.forEach { $0.dismissMode = .OnTap }
65+
case 0: dropDowns.forEach { $0.dismissMode = .automatic }
66+
case 1: dropDowns.forEach { $0.dismissMode = .onTap }
6767
default: break;
6868
}
6969
}
7070

71-
@IBAction func changeDirection(sender: UISegmentedControl) {
71+
@IBAction func changeDirection(_ sender: UISegmentedControl) {
7272
switch sender.selectedSegmentIndex {
73-
case 0: dropDowns.forEach { $0.direction = .Any }
74-
case 1: dropDowns.forEach { $0.direction = .Bottom }
75-
case 2: dropDowns.forEach { $0.direction = .Top }
73+
case 0: dropDowns.forEach { $0.direction = .any }
74+
case 1: dropDowns.forEach { $0.direction = .bottom }
75+
case 2: dropDowns.forEach { $0.direction = .top }
7676
default: break;
7777
}
7878
}
7979

80-
@IBAction func changeUI(sender: UISegmentedControl) {
80+
@IBAction func changeUI(_ sender: UISegmentedControl) {
8181
switch sender.selectedSegmentIndex {
8282
case 0: setupDefaultDropDown()
8383
case 1: customizeDropDown(self)
8484
default: break;
8585
}
8686
}
8787

88-
@IBAction func showKeyboard(sender: AnyObject) {
88+
@IBAction func showKeyboard(_ sender: AnyObject) {
8989
textField.becomeFirstResponder()
9090
}
9191

92-
@IBAction func hideKeyboard(sender: AnyObject) {
92+
@IBAction func hideKeyboard(_ sender: AnyObject) {
9393
view.endEditing(false)
9494
}
9595

9696
func setupDefaultDropDown() {
9797
DropDown.setupDefaultAppearance()
9898

9999
dropDowns.forEach {
100-
$0.cellNib = UINib(nibName: "DropDownCell", bundle: NSBundle(forClass: DropDownCell.self))
100+
$0.cellNib = UINib(nibName: "DropDownCell", bundle: Bundle(for: DropDownCell.self))
101101
$0.customCellConfiguration = nil
102102
}
103103
}
104104

105-
func customizeDropDown(sender: AnyObject) {
105+
func customizeDropDown(_ sender: AnyObject) {
106106
let appearance = DropDown.appearance()
107107

108108
appearance.cellHeight = 60
@@ -114,7 +114,7 @@ class ViewController: UIViewController {
114114
appearance.shadowOpacity = 0.9
115115
appearance.shadowRadius = 25
116116
appearance.animationduration = 0.25
117-
appearance.textColor = .darkGrayColor()
117+
appearance.textColor = .darkGray
118118
// appearance.textFont = UIFont(name: "Georgia", size: 14)
119119

120120
dropDowns.forEach {
@@ -137,8 +137,8 @@ class ViewController: UIViewController {
137137
super.viewDidLoad()
138138

139139
setupDropDowns()
140-
dropDowns.forEach { $0.dismissMode = .OnTap }
141-
dropDowns.forEach { $0.direction = .Any }
140+
dropDowns.forEach { $0.dismissMode = .onTap }
141+
dropDowns.forEach { $0.direction = .any }
142142

143143
view.addSubview(textField)
144144
}
@@ -175,7 +175,7 @@ class ViewController: UIViewController {
175175

176176
// Action triggered on selection
177177
chooseArticleDropDown.selectionAction = { [unowned self] (index, item) in
178-
self.chooseArticleButton.setTitle(item, forState: .Normal)
178+
self.chooseArticleButton.setTitle(item, for: .normal)
179179
}
180180

181181
// Action triggered on dropdown cancelation (hide)
@@ -215,7 +215,7 @@ class ViewController: UIViewController {
215215

216216
// Action triggered on selection
217217
amountDropDown.selectionAction = { [unowned self] (index, item) in
218-
self.amountButton.setTitle(item, forState: .Normal)
218+
self.amountButton.setTitle(item, for: .normal)
219219
}
220220
}
221221

@@ -236,7 +236,7 @@ class ViewController: UIViewController {
236236

237237
// Action triggered on selection
238238
chooseDropDown.selectionAction = { [unowned self] (index, item) in
239-
self.chooseButton.setTitle(item, forState: .Normal)
239+
self.chooseButton.setTitle(item, for: .normal)
240240
}
241241
}
242242

DropDown.xcodeproj/project.pbxproj

+9
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,16 @@
290290
TargetAttributes = {
291291
0A76440E1B676C2300BF1A2D = {
292292
CreatedOnToolsVersion = 6.4;
293+
LastSwiftMigration = 0800;
293294
};
294295
0A7644231B676C2300BF1A2D = {
295296
CreatedOnToolsVersion = 6.4;
297+
LastSwiftMigration = 0800;
296298
TestTargetID = 0A76440E1B676C2300BF1A2D;
297299
};
298300
0AB5D8701D0EEECD002D3A17 = {
299301
CreatedOnToolsVersion = 7.3.1;
302+
LastSwiftMigration = 0800;
300303
};
301304
};
302305
};
@@ -502,6 +505,7 @@
502505
PRODUCT_BUNDLE_IDENTIFIER = com.assistoLab.Demo;
503506
PRODUCT_NAME = "$(TARGET_NAME)";
504507
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
508+
SWIFT_VERSION = 3.0;
505509
TARGETED_DEVICE_FAMILY = "1,2";
506510
};
507511
name = Debug;
@@ -517,6 +521,7 @@
517521
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
518522
PRODUCT_BUNDLE_IDENTIFIER = com.assistoLab.Demo;
519523
PRODUCT_NAME = "$(TARGET_NAME)";
524+
SWIFT_VERSION = 3.0;
520525
TARGETED_DEVICE_FAMILY = "1,2";
521526
};
522527
name = Release;
@@ -533,6 +538,7 @@
533538
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
534539
PRODUCT_BUNDLE_IDENTIFIER = "com.kevin.hirsch.$(PRODUCT_NAME:rfc1034identifier)";
535540
PRODUCT_NAME = "$(TARGET_NAME)";
541+
SWIFT_VERSION = 3.0;
536542
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Demo.app/Demo";
537543
};
538544
name = Debug;
@@ -545,6 +551,7 @@
545551
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
546552
PRODUCT_BUNDLE_IDENTIFIER = "com.kevin.hirsch.$(PRODUCT_NAME:rfc1034identifier)";
547553
PRODUCT_NAME = "$(TARGET_NAME)";
554+
SWIFT_VERSION = 3.0;
548555
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Demo.app/Demo";
549556
};
550557
name = Release;
@@ -567,6 +574,7 @@
567574
PRODUCT_BUNDLE_IDENTIFIER = com.assistoLab.DropDown;
568575
PRODUCT_NAME = "$(TARGET_NAME)";
569576
SKIP_INSTALL = YES;
577+
SWIFT_VERSION = 3.0;
570578
TARGETED_DEVICE_FAMILY = "1,2";
571579
VERSIONING_SYSTEM = "apple-generic";
572580
VERSION_INFO_PREFIX = "";
@@ -589,6 +597,7 @@
589597
PRODUCT_BUNDLE_IDENTIFIER = com.assistoLab.DropDown;
590598
PRODUCT_NAME = "$(TARGET_NAME)";
591599
SKIP_INSTALL = YES;
600+
SWIFT_VERSION = 3.0;
592601
TARGETED_DEVICE_FAMILY = "1,2";
593602
VERSIONING_SYSTEM = "apple-generic";
594603
VERSION_INFO_PREFIX = "";

DropDown.xcodeproj/project.xcworkspace/xcshareddata/DropDown.xcscmblueprint

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "26F545BD-A325-473F-B7B3-A037533DDB11",
1111
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
1212
"E4EC3EC9984A396270972820B35B740A9BEF1CCB" : "",
13-
"6575E401F0E075B1FD6E179DAD8CCDF3853A7A8E" : "DropDown"
13+
"6575E401F0E075B1FD6E179DAD8CCDF3853A7A8E" : "DropDown\/"
1414
},
1515
"DVTSourceControlWorkspaceBlueprintNameKey" : "DropDown",
1616
"DVTSourceControlWorkspaceBlueprintVersion" : 204,

DropDown/helpers/DPDConstants.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ internal struct DPDConstant {
2424

2525
internal struct UI {
2626

27-
static let TextColor = UIColor.blackColor()
28-
static let TextFont = UIFont.systemFontOfSize(15)
27+
static let TextColor = UIColor.black
28+
static let TextFont = UIFont.systemFont(ofSize: 15)
2929
static let BackgroundColor = UIColor(white: 0.94, alpha: 1)
3030
static let SelectionBackgroundColor = UIColor(white: 0.89, alpha: 1)
31-
static let SeparatorColor = UIColor.clearColor()
31+
static let SeparatorColor = UIColor.clear
3232
static let CornerRadius: CGFloat = 2
3333
static let RowHeight: CGFloat = 44
3434
static let HeightPadding: CGFloat = 20
3535

3636
struct Shadow {
3737

38-
static let Color = UIColor.darkGrayColor()
38+
static let Color = UIColor.darkGray
3939
static let Offset = CGSize.zero
4040
static let Opacity: Float = 0.4
4141
static let Radius: CGFloat = 8
@@ -47,9 +47,9 @@ internal struct DPDConstant {
4747
internal struct Animation {
4848

4949
static let Duration = 0.15
50-
static let EntranceOptions: UIViewAnimationOptions = [.AllowUserInteraction, .CurveEaseOut]
51-
static let ExitOptions: UIViewAnimationOptions = [.AllowUserInteraction, .CurveEaseIn]
52-
static let DownScaleTransform = CGAffineTransformMakeScale(0.9, 0.9)
50+
static let EntranceOptions: UIViewAnimationOptions = [.allowUserInteraction, .curveEaseOut]
51+
static let ExitOptions: UIViewAnimationOptions = [.allowUserInteraction, .curveEaseIn]
52+
static let DownScaleTransform = CGAffineTransform(scaleX: 0.9, y: 0.9)
5353

5454
}
5555

DropDown/helpers/DPDKeyboardListener.swift

+15-15
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ internal final class KeyboardListener {
1212

1313
static let sharedInstance = KeyboardListener()
1414

15-
private(set) var isVisible = false
16-
private(set) var keyboardFrame = CGRectZero
17-
private var isListening = false
15+
fileprivate(set) var isVisible = false
16+
fileprivate(set) var keyboardFrame = CGRect.zero
17+
fileprivate var isListening = false
1818

1919
deinit {
2020
stopListeningToKeyboard()
@@ -33,36 +33,36 @@ extension KeyboardListener {
3333

3434
isListening = true
3535

36-
NSNotificationCenter.defaultCenter().addObserver(
36+
NotificationCenter.default.addObserver(
3737
self,
3838
selector: #selector(keyboardWillShow(_:)),
39-
name: UIKeyboardWillShowNotification,
39+
name: NSNotification.Name.UIKeyboardWillShow,
4040
object: nil)
41-
NSNotificationCenter.defaultCenter().addObserver(
41+
NotificationCenter.default.addObserver(
4242
self,
4343
selector: #selector(keyboardWillHide(_:)),
44-
name: UIKeyboardWillHideNotification,
44+
name: NSNotification.Name.UIKeyboardWillHide,
4545
object: nil)
4646
}
4747

4848
func stopListeningToKeyboard() {
49-
NSNotificationCenter.defaultCenter().removeObserver(self)
49+
NotificationCenter.default.removeObserver(self)
5050
}
5151

5252
@objc
53-
private func keyboardWillShow(notification: NSNotification) {
53+
fileprivate func keyboardWillShow(_ notification: Notification) {
5454
isVisible = true
55-
keyboardFrame = keyboardFrameFromNotification(notification)
55+
keyboardFrame = keyboardFrame(fromNotification: notification)
5656
}
5757

5858
@objc
59-
private func keyboardWillHide(notification: NSNotification) {
59+
fileprivate func keyboardWillHide(_ notification: Notification) {
6060
isVisible = false
61-
keyboardFrame = keyboardFrameFromNotification(notification)
61+
keyboardFrame = keyboardFrame(fromNotification: notification)
6262
}
6363

64-
private func keyboardFrameFromNotification(notification: NSNotification) -> CGRect {
65-
return (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() ?? CGRectZero
64+
fileprivate func keyboardFrame(fromNotification notification: Notification) -> CGRect {
65+
return ((notification as NSNotification).userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue ?? CGRect.zero
6666
}
6767

68-
}
68+
}

0 commit comments

Comments
 (0)