Skip to content

Instantly share code, notes, and snippets.

View andrehsouza's full-sized avatar

André Souza andrehsouza

View GitHub Profile
import UIKit
import CoreData
//MARK: - Core Data stack
class CoreDataStack: NSObject {
static let sharedInstance = CoreDataStack()
static let containerName = "CoreDataExample" //CoreDataExample.xcdatamodeld
private override init() {}
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
let managedContext = appDelegate.persistentContainer.viewContext
if let customer: Customer = NSEntityDescription.insertNewObject(forEntityName: "Customer", into: managedContext) as? Customer {
customer.name = "Mike"
//stuff
}
}
import Foundation
protocol DescribeProtocol: class { }
extension DescribeProtocol where Self: NSObject {
static var objectName: String {
return String(describing: self)
}
}
import Foundation
import CoreData
protocol FetchableProtocol: class {
associatedtype FetchableType: NSManagedObject = Self
}
extension FetchableProtocol where Self : NSManagedObject, FetchableType == Self {
static func fetchAll() -> [FetchableType] {
import Foundation
import CoreData
@objc(Customer)
class Customer: CustomNSManagedObject, FetchableProtocol {
@NSManaged var name: String
@NSManaged var email: String
@NSManaged var password: String
let customer = Customer()
customer.name = "Mike"
customer.email = "mike@gmail.com"
customer.password = "mike123"
let customerList = Customer.fetchAll()
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "CoreDataExample")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// UILabel
label.font = .preferredFont(forTextStyle: .headline)
label.adjustsFontForContentSizeCategory = true
// UIImageView
imageView.adjustsImageSizeForAccessibilityContentSizeCategory = true
view.accessibilityIgnoresInvertColors = true