Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 64775d1

Browse files
committed
Fix code indentation :)
1 parent 3bcc5d4 commit 64775d1

File tree

3 files changed

+352
-352
lines changed

3 files changed

+352
-352
lines changed

iOS8-Swift-TableView-CoreData-Example/AppDelegate.swift

Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -12,100 +12,100 @@ import CoreData
1212
@UIApplicationMain
1313
class AppDelegate: UIResponder, UIApplicationDelegate {
1414

15-
var window: UIWindow?
16-
17-
18-
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
19-
// Override point for customization after application launch.
20-
return true
21-
}
22-
23-
func applicationWillResignActive(application: UIApplication) {
24-
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
25-
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
26-
}
27-
28-
func applicationDidEnterBackground(application: UIApplication) {
29-
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
30-
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
31-
}
32-
33-
func applicationWillEnterForeground(application: UIApplication) {
34-
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
35-
}
36-
37-
func applicationDidBecomeActive(application: UIApplication) {
38-
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
39-
}
40-
41-
func applicationWillTerminate(application: UIApplication) {
42-
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
43-
// Saves changes in the application's managed object context before the application terminates.
44-
self.saveContext()
45-
}
46-
47-
// MARK: - Core Data stack
48-
49-
lazy var applicationDocumentsDirectory: NSURL = {
50-
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.diegorv.iOS8_Swift_TableView_CoreData_Example" in the application's documents Application Support directory.
51-
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
52-
return urls[urls.count-1] as! NSURL
53-
}()
54-
55-
lazy var managedObjectModel: NSManagedObjectModel = {
56-
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
57-
let modelURL = NSBundle.mainBundle().URLForResource("iOS8_Swift_TableView_CoreData_Example", withExtension: "momd")!
58-
return NSManagedObjectModel(contentsOfURL: modelURL)!
59-
}()
60-
61-
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
62-
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
63-
// Create the coordinator and store
64-
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
65-
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("iOS8_Swift_TableView_CoreData_Example.sqlite")
66-
var error: NSError? = nil
67-
var failureReason = "There was an error creating or loading the application's saved data."
68-
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
69-
coordinator = nil
70-
// Report any error we got.
71-
var dict = [String: AnyObject]()
72-
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
73-
dict[NSLocalizedFailureReasonErrorKey] = failureReason
74-
dict[NSUnderlyingErrorKey] = error
75-
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
76-
// Replace this with code to handle the error appropriately.
77-
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
78-
NSLog("Unresolved error \(error), \(error!.userInfo)")
79-
abort()
80-
}
81-
82-
return coordinator
83-
}()
84-
85-
lazy var managedObjectContext: NSManagedObjectContext? = {
86-
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
87-
let coordinator = self.persistentStoreCoordinator
88-
if coordinator == nil {
89-
return nil
90-
}
91-
var managedObjectContext = NSManagedObjectContext()
92-
managedObjectContext.persistentStoreCoordinator = coordinator
93-
return managedObjectContext
94-
}()
95-
96-
// MARK: - Core Data Saving support
97-
98-
func saveContext () {
99-
if let moc = self.managedObjectContext {
100-
var error: NSError? = nil
101-
if moc.hasChanges && !moc.save(&error) {
102-
// Replace this implementation with code to handle the error appropriately.
103-
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
104-
NSLog("Unresolved error \(error), \(error!.userInfo)")
105-
abort()
106-
}
107-
}
108-
}
15+
var window: UIWindow?
16+
17+
18+
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
19+
// Override point for customization after application launch.
20+
return true
21+
}
22+
23+
func applicationWillResignActive(application: UIApplication) {
24+
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
25+
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
26+
}
27+
28+
func applicationDidEnterBackground(application: UIApplication) {
29+
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
30+
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
31+
}
32+
33+
func applicationWillEnterForeground(application: UIApplication) {
34+
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
35+
}
36+
37+
func applicationDidBecomeActive(application: UIApplication) {
38+
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
39+
}
40+
41+
func applicationWillTerminate(application: UIApplication) {
42+
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
43+
// Saves changes in the application's managed object context before the application terminates.
44+
self.saveContext()
45+
}
46+
47+
// MARK: - Core Data stack
48+
49+
lazy var applicationDocumentsDirectory: NSURL = {
50+
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.diegorv.iOS8_Swift_TableView_CoreData_Example" in the application's documents Application Support directory.
51+
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
52+
return urls[urls.count-1] as! NSURL
53+
}()
54+
55+
lazy var managedObjectModel: NSManagedObjectModel = {
56+
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
57+
let modelURL = NSBundle.mainBundle().URLForResource("iOS8_Swift_TableView_CoreData_Example", withExtension: "momd")!
58+
return NSManagedObjectModel(contentsOfURL: modelURL)!
59+
}()
60+
61+
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
62+
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
63+
// Create the coordinator and store
64+
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
65+
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("iOS8_Swift_TableView_CoreData_Example.sqlite")
66+
var error: NSError? = nil
67+
var failureReason = "There was an error creating or loading the application's saved data."
68+
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
69+
coordinator = nil
70+
// Report any error we got.
71+
var dict = [String: AnyObject]()
72+
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
73+
dict[NSLocalizedFailureReasonErrorKey] = failureReason
74+
dict[NSUnderlyingErrorKey] = error
75+
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
76+
// Replace this with code to handle the error appropriately.
77+
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
78+
NSLog("Unresolved error \(error), \(error!.userInfo)")
79+
abort()
80+
}
81+
82+
return coordinator
83+
}()
84+
85+
lazy var managedObjectContext: NSManagedObjectContext? = {
86+
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
87+
let coordinator = self.persistentStoreCoordinator
88+
if coordinator == nil {
89+
return nil
90+
}
91+
var managedObjectContext = NSManagedObjectContext()
92+
managedObjectContext.persistentStoreCoordinator = coordinator
93+
return managedObjectContext
94+
}()
95+
96+
// MARK: - Core Data Saving support
97+
98+
func saveContext () {
99+
if let moc = self.managedObjectContext {
100+
var error: NSError? = nil
101+
if moc.hasChanges && !moc.save(&error) {
102+
// Replace this implementation with code to handle the error appropriately.
103+
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
104+
NSLog("Unresolved error \(error), \(error!.userInfo)")
105+
abort()
106+
}
107+
}
108+
}
109109

110110
}
111111

iOS8-Swift-TableView-CoreData-Example/Item.swift

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,72 +11,72 @@ import CoreData
1111

1212
// MODEL
1313
class Item: NSManagedObject {
14-
15-
@NSManaged var name: String
16-
17-
/// Function to initialize a new Item
18-
convenience init(name: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) {
19-
let entity = NSEntityDescription.entityForName("Item", inManagedObjectContext: managedObjectContext)!
20-
self.init(entity: entity, insertIntoManagedObjectContext: managedObjectContext)
21-
self.name = name
22-
}
23-
24-
/// Function to get all CoreData values
25-
///
26-
/// :param: managedObjectContext CoreData Connection
27-
///
28-
class func fetchAll(managedObjectContext: NSManagedObjectContext) -> [Item] {
29-
let listagemCoreData = NSFetchRequest(entityName: "Item")
30-
31-
// Sort alphabetical by field "name"
32-
let orderByName = NSSortDescriptor(key: "name", ascending: true, selector: "caseInsensitiveCompare:")
33-
listagemCoreData.sortDescriptors = [orderByName]
34-
35-
// Get items from CoreData
36-
return managedObjectContext.executeFetchRequest(listagemCoreData, error: nil) as? [Item] ?? []
37-
}
38-
39-
/// Function to search item by name
40-
///
41-
/// :param: name Item name
42-
/// :param: managedObjectContext CoreData Connection
43-
///
44-
class func search(name: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> Item? {
45-
let fetchRequest = NSFetchRequest(entityName: "Item")
46-
fetchRequest.predicate = NSPredicate(format: "name = %@", name)
47-
48-
let result = managedObjectContext.executeFetchRequest(fetchRequest, error: nil) as? [Item]
49-
return result?.first
50-
}
51-
52-
/// Function to check duplicate item
53-
///
54-
/// :param: name Item name
55-
/// :param: managedObjectContext CoreData Connection
56-
///
57-
class func checkDuplicate(name: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> Bool {
58-
return search(name, inManagedObjectContext: managedObjectContext) != nil
59-
}
6014

61-
/// Function to delete a item
62-
///
63-
/// :param: managedObjectContext CoreData Connection
64-
///
65-
func destroy(managedObjectContext: NSManagedObjectContext) {
66-
managedObjectContext.deleteObject(self)
67-
}
68-
69-
/// Function to save CoreData values
70-
///
71-
/// :param: managedObjectContext CoreData Connection
72-
///
73-
func save(managedObjectContext: NSManagedObjectContext) {
74-
var error: NSErrorPointer = nil
75-
76-
if(managedObjectContext.save(error)) {
77-
if (error != nil) {
78-
println("Error on save: \(error.debugDescription)")
79-
}
80-
}
81-
}
15+
@NSManaged var name: String
16+
17+
/// Function to initialize a new Item
18+
convenience init(name: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) {
19+
let entity = NSEntityDescription.entityForName("Item", inManagedObjectContext: managedObjectContext)!
20+
self.init(entity: entity, insertIntoManagedObjectContext: managedObjectContext)
21+
self.name = name
22+
}
23+
24+
/// Function to get all CoreData values
25+
///
26+
/// :param: managedObjectContext CoreData Connection
27+
///
28+
class func fetchAll(managedObjectContext: NSManagedObjectContext) -> [Item] {
29+
let listagemCoreData = NSFetchRequest(entityName: "Item")
30+
31+
// Sort alphabetical by field "name"
32+
let orderByName = NSSortDescriptor(key: "name", ascending: true, selector: "caseInsensitiveCompare:")
33+
listagemCoreData.sortDescriptors = [orderByName]
34+
35+
// Get items from CoreData
36+
return managedObjectContext.executeFetchRequest(listagemCoreData, error: nil) as? [Item] ?? []
37+
}
38+
39+
/// Function to search item by name
40+
///
41+
/// :param: name Item name
42+
/// :param: managedObjectContext CoreData Connection
43+
///
44+
class func search(name: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> Item? {
45+
let fetchRequest = NSFetchRequest(entityName: "Item")
46+
fetchRequest.predicate = NSPredicate(format: "name = %@", name)
47+
48+
let result = managedObjectContext.executeFetchRequest(fetchRequest, error: nil) as? [Item]
49+
return result?.first
50+
}
51+
52+
/// Function to check duplicate item
53+
///
54+
/// :param: name Item name
55+
/// :param: managedObjectContext CoreData Connection
56+
///
57+
class func checkDuplicate(name: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> Bool {
58+
return search(name, inManagedObjectContext: managedObjectContext) != nil
59+
}
60+
61+
/// Function to delete a item
62+
///
63+
/// :param: managedObjectContext CoreData Connection
64+
///
65+
func destroy(managedObjectContext: NSManagedObjectContext) {
66+
managedObjectContext.deleteObject(self)
67+
}
68+
69+
/// Function to save CoreData values
70+
///
71+
/// :param: managedObjectContext CoreData Connection
72+
///
73+
func save(managedObjectContext: NSManagedObjectContext) {
74+
var error: NSErrorPointer = nil
75+
76+
if(managedObjectContext.save(error)) {
77+
if (error != nil) {
78+
println("Error on save: \(error.debugDescription)")
79+
}
80+
}
81+
}
8282
}

0 commit comments

Comments
 (0)