Skip to content
This repository was archived by the owner on Jan 16, 2021. It is now read-only.

Commit 423d464

Browse files
committed
Merge pull request #156 from ParsePlatform/nlutsenko.swift2
Update ParseUIDemo-Swift to Swift 2.
2 parents 721e266 + b63dc71 commit 423d464

File tree

11 files changed

+81
-72
lines changed

11 files changed

+81
-72
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ script:
1313
- set -o pipefail
1414
- xcodebuild -workspace ParseUI.xcworkspace -scheme 'ParseUI' -sdk iphonesimulator build | xcpretty -c
1515
- xcodebuild -workspace ParseUI.xcworkspace -scheme 'ParseUIDemo' -sdk iphonesimulator build | xcpretty -c
16+
- xcodebuild -workspace ParseUI.xcworkspace -scheme 'ParseUIDemo-Swift' -sdk iphonesimulator build | xcpretty -c

ParseUI.xcodeproj/project.pbxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@
855855
isa = PBXProject;
856856
attributes = {
857857
CLASSPREFIX = PF;
858+
LastSwiftUpdateCheck = 0700;
858859
LastUpgradeCheck = 0620;
859860
ORGANIZATIONNAME = "Parse Inc.";
860861
TargetAttributes = {

ParseUIDemo/Swift/AppDelegate.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4747
return true
4848
}
4949

50-
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
50+
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
5151
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
5252
}
5353

@@ -71,27 +71,27 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
7171

7272
var objects: [PFObject] = Array()
7373

74-
let query = PFQuery(className: "Todo")
75-
if let todos = query.findObjects() as? [PFObject] {
74+
do {
75+
let todos = try PFQuery(className: "Todo").findObjects()
7676
if todos.count == 0 {
77-
for (index, title) in enumerate(todoTitles) {
77+
for (index, title) in todoTitles.enumerate() {
7878
let todo = PFObject(className: "Todo")
7979
todo["title"] = title
8080
todo["priority"] = index % 3
8181
objects.append(todo)
8282
}
8383
}
84-
}
84+
} catch {}
8585

8686
let appNames = [ "Anypic", "Anywall", "f8" ]
87-
let appsQuery = PFQuery(className: "App")
88-
if let apps = appsQuery.findObjects() as? [PFObject] {
87+
do {
88+
let apps = try PFQuery(className: "App").findObjects()
8989
if apps.count == 0 {
90-
for (index, appName) in enumerate(appNames) {
90+
for (index, appName) in appNames.enumerate() {
9191
let bundle = NSBundle.mainBundle()
92-
if let filePath = bundle.pathForResource(String(index), ofType: "png") {
93-
if let data = NSData(contentsOfFile: filePath) {
94-
let file = PFFile(name: filePath.lastPathComponent, data: data)
92+
if let fileURL = bundle.URLForResource(String(index), withExtension: "png") {
93+
if let data = NSData(contentsOfURL: fileURL) {
94+
let file = PFFile(name: fileURL.lastPathComponent, data: data)
9595
let object = PFObject(className: "App")
9696
object["icon"] = file
9797
object["name"] = appName
@@ -100,10 +100,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
100100
}
101101
}
102102
}
103-
}
103+
} catch {}
104104

105105
if objects.count != 0 {
106-
PFObject.saveAll(objects)
106+
do {
107+
try PFObject.saveAll(objects)
108+
} catch {}
107109
}
108110
}
109111

ParseUIDemo/Swift/CustomViewControllers/QueryCollectionViewController/DeletionCollectionViewController.swift

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,22 @@ class DeletionCollectionViewController: PFQueryCollectionViewController, UIAlert
6060

6161
@objc
6262
func addTodo() {
63-
if NSClassFromString("UIAlertController") != nil {
63+
if #available(iOS 8.0, *) {
6464
let alertDialog = UIAlertController(title: "Add Todo", message: nil, preferredStyle: .Alert)
6565

66-
var titleTextField : UITextField! = nil
66+
var titleTextField : UITextField? = nil
6767
alertDialog.addTextFieldWithConfigurationHandler() {
6868
titleTextField = $0
6969
}
7070

7171
alertDialog.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
7272
alertDialog.addAction(UIAlertAction(title: "Save", style: .Default) { (action) -> Void in
73-
let object = PFObject(className: self.parseClassName!, dictionary: [ "title": titleTextField.text ])
74-
object.saveEventually().continueWithSuccessBlock({ (_) -> AnyObject! in
75-
return self.loadObjects()
76-
})
73+
if let title = titleTextField?.text {
74+
let object = PFObject(className: self.parseClassName!, dictionary: [ "title": title ])
75+
object.saveEventually().continueWithSuccessBlock { (_) -> AnyObject! in
76+
return self.loadObjects()
77+
}
78+
}
7779
})
7880

7981
presentViewController(alertDialog, animated: true, completion: nil)
@@ -88,7 +90,7 @@ class DeletionCollectionViewController: PFQueryCollectionViewController, UIAlert
8890

8991
alertView.alertViewStyle = .PlainTextInput
9092
alertView.textFieldAtIndex(0)?.placeholder = "Name"
91-
93+
9294
alertView.show()
9395
}
9496
}
@@ -119,13 +121,15 @@ class DeletionCollectionViewController: PFQueryCollectionViewController, UIAlert
119121
return
120122
}
121123

122-
let object = PFObject(
123-
className: self.parseClassName!,
124-
dictionary: [ "title": alertView.textFieldAtIndex(0)!.text ]
125-
)
126-
127-
object.saveEventually().continueWithSuccessBlock({ (_) -> AnyObject! in
128-
return self.loadObjects()
129-
})
124+
if let title = alertView.textFieldAtIndex(0)?.text {
125+
let object = PFObject(
126+
className: self.parseClassName!,
127+
dictionary: [ "title": title ]
128+
)
129+
130+
object.saveEventually().continueWithSuccessBlock({ (_) -> AnyObject! in
131+
return self.loadObjects()
132+
})
133+
}
130134
}
131135
}

ParseUIDemo/Swift/CustomViewControllers/QueryCollectionViewController/PaginatedCollectionViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class PaginatedCollectionViewController: PFQueryCollectionViewController {
6666

6767
if let title = object?["title"] as? String {
6868
let attributedTitle = NSMutableAttributedString(string: title)
69-
if var priority = object?["priority"] as? Int {
69+
if let priority = object?["priority"] as? Int {
7070
let attributes = [NSFontAttributeName : UIFont.systemFontOfSize(13.0), NSForegroundColorAttributeName : UIColor.grayColor()]
7171
let string = NSAttributedString(string: "\nPriority: \(priority)", attributes: attributes)
7272
attributedTitle.appendAttributedString(string)

ParseUIDemo/Swift/CustomViewControllers/QueryCollectionViewController/SectionedCollectionViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SimpleCollectionReusableView : UICollectionReusableView {
3434
addSubview(label)
3535
}
3636

37-
required init(coder decoder: NSCoder) {
37+
required init?(coder decoder: NSCoder) {
3838
super.init(coder: decoder)
3939

4040
label.textAlignment = .Center
@@ -97,7 +97,7 @@ class SectionedCollectionViewController: PFQueryCollectionViewController {
9797
sections[priority] = array
9898
}
9999
}
100-
sectionKeys = sections.keys.array.sorted(<)
100+
sectionKeys = sections.keys.sort(<)
101101

102102
collectionView?.reloadData()
103103
}
@@ -112,7 +112,7 @@ class SectionedCollectionViewController: PFQueryCollectionViewController {
112112

113113
}
114114

115-
extension SectionedCollectionViewController : UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
115+
extension SectionedCollectionViewController {
116116

117117
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
118118
return sections.count

ParseUIDemo/Swift/CustomViewControllers/QueryCollectionViewController/SimpleCollectionViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class SimpleCollectionViewController: PFQueryCollectionViewController {
6565

6666
if let title = object?["title"] as? String {
6767
let attributedTitle = NSMutableAttributedString(string: title)
68-
if var priority = object?["priority"] as? Int {
68+
if let priority = object?["priority"] as? Int {
6969
let attributes = [NSFontAttributeName : UIFont.systemFontOfSize(13.0), NSForegroundColorAttributeName : UIColor.grayColor()]
7070
let string = NSAttributedString(string: "\nPriority: \(priority)", attributes: attributes)
7171
attributedTitle.appendAttributedString(string)

ParseUIDemo/Swift/CustomViewControllers/QueryCollectionViewController/StoryboardCollectionViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class StoryboardCollectionViewController: PFQueryCollectionViewController {
6161

6262
if let title = object?["title"] as? String {
6363
let attributedTitle = NSMutableAttributedString(string: title)
64-
if var priority = object?["priority"] as? Int {
64+
if let priority = object?["priority"] as? Int {
6565
let attributes = [NSFontAttributeName : UIFont.systemFontOfSize(13.0), NSForegroundColorAttributeName : UIColor.grayColor()]
6666
let string = NSAttributedString(string: "\nPriority: \(priority)", attributes: attributes)
6767
attributedTitle.appendAttributedString(string)

ParseUIDemo/Swift/CustomViewControllers/QueryTableViewController/DeletionTableViewController.swift

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class DeletionTableViewController: PFQueryTableViewController, UIAlertViewDelega
5959

6060
@objc
6161
func addTodo() {
62-
if NSClassFromString("UIAlertController") != nil {
62+
63+
if #available(iOS 8.0, *) {
6364
let alertDialog = UIAlertController(title: "Add Todo", message: nil, preferredStyle: .Alert)
6465

6566
var titleTextField : UITextField! = nil
@@ -69,10 +70,12 @@ class DeletionTableViewController: PFQueryTableViewController, UIAlertViewDelega
6970

7071
alertDialog.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
7172
alertDialog.addAction(UIAlertAction(title: "Save", style: .Default) { (action) -> Void in
72-
let object = PFObject(className: self.parseClassName!, dictionary: [ "title": titleTextField.text ])
73-
object.saveInBackground().continueWithSuccessBlock({ (_) -> AnyObject! in
74-
return self.loadObjects()
75-
})
73+
if let title = titleTextField.text {
74+
let object = PFObject(className: self.parseClassName!, dictionary: [ "title": title ])
75+
object.saveInBackground().continueWithSuccessBlock({ (_) -> AnyObject! in
76+
return self.loadObjects()
77+
})
78+
}
7679
})
7780

7881
presentViewController(alertDialog, animated: true, completion: nil)
@@ -94,7 +97,7 @@ class DeletionTableViewController: PFQueryTableViewController, UIAlertViewDelega
9497

9598
@objc
9699
func deleteSelectedItems() {
97-
removeObjectsAtIndexPaths(tableView.indexPathsForSelectedRows())
100+
removeObjectsAtIndexPaths(tableView.indexPathsForSelectedRows)
98101
}
99102

100103
// MARK - UIAlertViewDelegate
@@ -105,13 +108,11 @@ class DeletionTableViewController: PFQueryTableViewController, UIAlertViewDelega
105108
return
106109
}
107110

108-
let object = PFObject(
109-
className: self.parseClassName!,
110-
dictionary: [ "title": alertView.textFieldAtIndex(0)!.text ]
111-
)
112-
113-
object.saveEventually().continueWithSuccessBlock({ (_) -> AnyObject! in
114-
return self.loadObjects()
115-
})
111+
if let title = alertView.textFieldAtIndex(0)?.text {
112+
let object = PFObject(className: self.parseClassName!, dictionary: [ "title": title ])
113+
object.saveEventually().continueWithSuccessBlock({ (_) -> AnyObject! in
114+
return self.loadObjects()
115+
})
116+
}
116117
}
117118
}

ParseUIDemo/Swift/CustomViewControllers/QueryTableViewController/SectionedTableViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class SectionedTableViewController: PFQueryTableViewController {
5252
sections[priority] = array
5353
}
5454
}
55-
sectionKeys = sections.keys.array.sorted(<)
55+
sectionKeys = sections.keys.sort(<)
5656

5757
tableView.reloadData()
5858
}
@@ -66,7 +66,7 @@ class SectionedTableViewController: PFQueryTableViewController {
6666
}
6767
}
6868

69-
extension SectionedTableViewController : UITableViewDataSource {
69+
extension SectionedTableViewController {
7070

7171
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
7272
return sections.count

0 commit comments

Comments
 (0)