Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
Migrate to Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis committed Jun 29, 2016
1 parent 7328e34 commit 4705977
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 116 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "3lvis/DATAStack"
github "3lvis/DATAStack" "6.0.0-beta1"
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "3lvis/DATAStack" "5.2.1"
github "3lvis/DATAStack" "6.0.0-beta1"
2 changes: 1 addition & 1 deletion CollectionObjC/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ - (void)viewDidLoad {
}

- (void)addAction {
[self.dataStack performInNewBackgroundContext:^(NSManagedObjectContext *backgroundContext) {
[self.dataStack performInNewBackgroundContext:^(NSManagedObjectContext *backgroundContext) {
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User"
inManagedObjectContext:backgroundContext];
NSManagedObject *user = [[NSManagedObject alloc] initWithEntity:entity
Expand Down
6 changes: 3 additions & 3 deletions CollectionSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import DATAStack
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow? = {
let window = UIWindow(frame: UIScreen.mainScreen().bounds)
let window = UIWindow(frame: UIScreen.main().bounds)

return window
}()
Expand All @@ -17,9 +17,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return dataStack
}()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let window = self.window {
let bounds = UIScreen.mainScreen().bounds
let bounds = UIScreen.main().bounds

let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 120, height: 120)
Expand Down
33 changes: 16 additions & 17 deletions CollectionSwift/CollectionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class CollectionController: UICollectionViewController {
lazy var dataSource: DATASource = {
guard let collectionView = self.collectionView else { fatalError("CollectionView is nil") }

let request: NSFetchRequest = NSFetchRequest(entityName: "User")
let request: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
request.sortDescriptors = [
NSSortDescriptor(key: "name", ascending: true),
NSSortDescriptor(key: "firstLetterOfName", ascending: true)
SortDescriptor(key: "name", ascending: true),
SortDescriptor(key: "firstLetterOfName", ascending: true)
]

let dataSource = DATASource(collectionView: collectionView, cellIdentifier: CollectionCell.Identifier, fetchRequest: request, mainContext: self.dataStack.mainContext, sectionName: "firstLetterOfName", configuration: { cell, item, indexPath in
let collectionCell = cell as! CollectionCell
collectionCell.textLabel.text = item.valueForKey("name") as? String
collectionCell.textLabel.text = item.value(forKey: "name") as? String
})

return dataSource
Expand All @@ -38,24 +38,23 @@ class CollectionController: UICollectionViewController {

guard let collectionView = self.collectionView else { fatalError("CollectionView is nil") }

collectionView.registerClass(CollectionCell.self, forCellWithReuseIdentifier: CollectionCell.Identifier)
collectionView.register(CollectionCell.self, forCellWithReuseIdentifier: CollectionCell.Identifier)
collectionView.dataSource = self.dataSource
collectionView.backgroundColor = UIColor.whiteColor()
collectionView.backgroundColor = UIColor.white()
collectionView.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)

self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(CollectionController.saveAction))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(CollectionController.saveAction))
}

func saveAction() {
self.dataStack.performInNewBackgroundContext { backgroundContext in
if let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: backgroundContext) {
let user = NSManagedObject(entity: entity, insertIntoManagedObjectContext: backgroundContext)
if let entity = NSEntityDescription.entity(forEntityName: "User", in: backgroundContext) {
let user = NSManagedObject(entity: entity, insertInto: backgroundContext)

let name = self.randomString()
let firstLetter = String(Array(name.characters)[0])
let firstLetter = String(name[name.startIndex])
user.setValue(name, forKey: "name")
user.setValue(firstLetter.uppercaseString, forKey: "firstLetterOfName")

user.setValue(firstLetter.uppercased(), forKey: "firstLetterOfName")
do {
try backgroundContext.save()
} catch let savingError as NSError {
Expand Down Expand Up @@ -84,19 +83,19 @@ class CollectionController: UICollectionViewController {
}

extension CollectionController {
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let object = self.dataSource.objectAtIndexPath(indexPath) else { return }

if let name = object.valueForKey("name") as? String where name.characters.first == "A" {
if let name = object.value(forKey: "name") as? String where name.characters.first == "A" {
self.dataStack.performInNewBackgroundContext({ backgroundContext in
let backgroundObject = backgroundContext.objectWithID(object.objectID)
let backgroundObject = backgroundContext.object(with: object.objectID)
backgroundObject.setValue(name + "+", forKey: "name")
try! backgroundContext.save()
})
} else {
self.dataStack.performInNewBackgroundContext({ backgroundContext in
let backgroundObject = backgroundContext.objectWithID(object.objectID)
backgroundContext.deleteObject(backgroundObject)
let backgroundObject = backgroundContext.object(with: object.objectID)
backgroundContext.delete(backgroundObject)
try! backgroundContext.save()
})
}
Expand Down
4 changes: 2 additions & 2 deletions DATASourceDelegateTableSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import DATAStack
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow? = {
let window = UIWindow(frame: UIScreen.mainScreen().bounds)
let window = UIWindow(frame: UIScreen.main().bounds)

return window
}()
Expand All @@ -17,7 +17,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return dataStack
}()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let window = self.window {
let viewController = ViewController(dataStack: self.dataStack)
window.rootViewController = UINavigationController(rootViewController: viewController)
Expand Down
30 changes: 15 additions & 15 deletions DATASourceDelegateTableSwift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class ViewController: UITableViewController {
var dataStack: DATAStack?

lazy var dataSource: DATASource = {
let request: NSFetchRequest = NSFetchRequest(entityName: "User")
request.sortDescriptors = [NSSortDescriptor(key: "createdDate", ascending: true)]
let request: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
request.sortDescriptors = [SortDescriptor(key: "createdDate", ascending: true)]

let dataSource = DATASource(tableView: self.tableView, cellIdentifier: ViewController.CellIdentifier, fetchRequest: request, mainContext: self.dataStack!.mainContext, configuration: { cell, item, indexPath in
let date = item.valueForKey("createdDate") as? NSDate
let date = item.value(forKey: "createdDate") as? NSDate
cell.textLabel?.text = date?.description
})

Expand All @@ -21,53 +21,53 @@ class ViewController: UITableViewController {
}()

convenience init(dataStack: DATAStack) {
self.init(style: .Plain)
self.init(style: .plain)

self.dataStack = dataStack
}

override func viewDidLoad() {
super.viewDidLoad()

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: ViewController.CellIdentifier)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(ViewController.saveAction))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: #selector(ViewController.editAction))
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: ViewController.CellIdentifier)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(ViewController.saveAction))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(ViewController.editAction))
self.tableView.dataSource = self.dataSource
self.tableView.delegate = self
}

func saveAction() {
self.dataStack!.performInNewBackgroundContext { backgroundContext in
let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: backgroundContext)!
let user = NSManagedObject(entity: entity, insertIntoManagedObjectContext: backgroundContext)
let entity = NSEntityDescription.entity(forEntityName: "User", in: backgroundContext)!
let user = NSManagedObject(entity: entity, insertInto: backgroundContext)
user.setValue(NSDate(), forKey: "createdDate")
try! backgroundContext.save()
}
}

func editAction() {
self.setEditing(!self.editing, animated: true)
self.setEditing(!self.isEditing, animated: true)
}
}

extension ViewController: DATASourceDelegate {
func dataSource(dataSource: DATASource, tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
func dataSource(_ dataSource: DATASource, tableView: UITableView, canEditRowAtIndexPath indexPath: IndexPath) -> Bool {
return true
}

// This doesn't seem to be needed when implementing tableView(_:editActionsForRowAtIndexPath).
func dataSource(dataSource: DATASource, tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
func dataSource(_ dataSource: DATASource, tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: IndexPath) {

}
}

// MARK: - UITableViewDelegate

extension ViewController {
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .Default, title: "Delete") { action, indexPath in
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .default, title: "Delete") { action, indexPath in
let item = self.dataSource.objectAtIndexPath(indexPath)!
self.dataStack!.mainContext.deleteObject(item)
self.dataStack!.mainContext.delete(item)
try! self.dataStack!.mainContext.save()
}

Expand Down
26 changes: 26 additions & 0 deletions Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -832,9 +832,13 @@
CreatedOnToolsVersion = 7.2;
DevelopmentTeam = C6K65RDJHL;
DevelopmentTeamName = "Elvis Nunez (Personal Team)";
LastSwiftMigration = 0800;
};
146D72AB1AB782920058798C = {
CreatedOnToolsVersion = 6.2;
DevelopmentTeam = C6K65RDJHL;
DevelopmentTeamName = "Elvis Nunez (Personal Team)";
LastSwiftMigration = 0800;
};
149AF18F1C0EB9EF00BA0907 = {
CreatedOnToolsVersion = 7.1;
Expand All @@ -846,26 +850,31 @@
CreatedOnToolsVersion = 7.1;
DevelopmentTeam = C6K65RDJHL;
DevelopmentTeamName = "Elvis Nunez (Personal Team)";
LastSwiftMigration = 0800;
};
14F3A68D1BE01B9200789E22 = {
CreatedOnToolsVersion = 7.1;
DevelopmentTeam = C6K65RDJHL;
DevelopmentTeamName = "Elvis Nunez (Personal Team)";
LastSwiftMigration = 0800;
};
14F3A6A51BE01B9C00789E22 = {
CreatedOnToolsVersion = 7.1;
DevelopmentTeam = C6K65RDJHL;
DevelopmentTeamName = "Elvis Nunez (Personal Team)";
LastSwiftMigration = 0800;
};
14F3A6B91BE01BA900789E22 = {
CreatedOnToolsVersion = 7.1;
DevelopmentTeam = C6K65RDJHL;
DevelopmentTeamName = "Elvis Nunez (Personal Team)";
LastSwiftMigration = 0800;
};
14F535911C3A7E9C00671EDF = {
CreatedOnToolsVersion = 7.2;
DevelopmentTeam = C6K65RDJHL;
DevelopmentTeamName = "Elvis Nunez (Personal Team)";
LastSwiftMigration = 0800;
};
14F848661CC667990052E4B0 = {
CreatedOnToolsVersion = 7.3;
Expand All @@ -877,6 +886,7 @@
CreatedOnToolsVersion = 7.3;
DevelopmentTeam = C6K65RDJHL;
DevelopmentTeamName = "Elvis Nunez (Personal Team)";
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -1246,6 +1256,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.demo.InfiniteCollectionSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -1265,6 +1276,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.demo.InfiniteCollectionSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down Expand Up @@ -1361,6 +1373,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.sample.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -1374,6 +1387,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.sample.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down Expand Up @@ -1433,6 +1447,7 @@
PRODUCT_BUNDLE_IDENTIFIER = demo.CollectionSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -1452,6 +1467,7 @@
PRODUCT_BUNDLE_IDENTIFIER = demo.CollectionSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -1473,6 +1489,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_INSTALL_OBJC_HEADER = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -1494,6 +1511,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_INSTALL_OBJC_HEADER = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -1513,6 +1531,7 @@
PRODUCT_BUNDLE_IDENTIFIER = demo.TableSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -1532,6 +1551,7 @@
PRODUCT_BUNDLE_IDENTIFIER = demo.TableSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -1552,6 +1572,7 @@
PRODUCT_BUNDLE_IDENTIFIER = demo.TableObjC;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -1572,6 +1593,7 @@
PRODUCT_BUNDLE_IDENTIFIER = demo.TableObjC;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -1591,6 +1613,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.demo.InfiniteTableSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -1610,6 +1633,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.demo.InfiniteTableSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down Expand Up @@ -1684,6 +1708,7 @@
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 9.0;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -1710,6 +1735,7 @@
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 9.0;
VERSIONING_SYSTEM = "apple-generic";
Expand Down
Loading

0 comments on commit 4705977

Please sign in to comment.