Skip to content

Instantly share code, notes, and snippets.

View barbaramartina's full-sized avatar

Bar barbaramartina

View GitHub Profile
@barbaramartina
barbaramartina / unit-tests-templates.swift
Created May 26, 2016 10:53
Swift: Unit testing templates
// see: https://www.youtube.com/watch?v=Y3t8e_rUyTk&list=PLwu90VQm-yTobHYmgVdvZu5sBdyPPxW9S and http://ladyandtech.blogspot.de/2016/03/swift-testing-with-xctest.html
import XCTest
@testable import YOUR_MODULE_NAME
class yourTestCase: XCTestCase {
override func setUp() {
@barbaramartina
barbaramartina / Swift 3: How to implement a service extension.swift
Created November 5, 2016 11:10
Swift 3: implement a service extension and download an image attachment.
//
// NotificationService.swift
// Service
//
// Created by Barbara Rodeker on 28/09/16.
// Copyright © 2016 Barbara Rodeker. All rights reserved.
//
import UserNotifications
@barbaramartina
barbaramartina / SimpleObserver.swift
Created April 8, 2016 14:56
How to create an observer of operations and queues in Swift. Which properties you can observe + docs links
//
// An Observer class intended to show how KVO works on operations and queues
//
// Created by Barbara Rodeker on 3/3/16.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
@barbaramartina
barbaramartina / AsynchronousOperation.swift
Created April 8, 2016 09:07
A documented subclass of an asynchronous NSOperation in Swift
//
// This is an implementation of an asynchronous operation subclass.
// BASED ON: ConcurrentOperation.swift: Created by Caleb Davenport on 7/7/14.
// The original implementation has been completed with specific links to the documentation in order
// to make sure all the guidelines for subclassing an async operation are followed and learn more about
// how it should be correctly implemented, as well as having links available to help our memory to remember all
// the requirements a subclass of NSOperation should fullfit when it runs asynchronously.
//
// From Apple Docs:
// If you are creating a concurrent operation, you need to override the following methods and properties at a minimum:
@barbaramartina
barbaramartina / Swift 3: Advanced Notifications: Example of a Content Extension.swift
Created November 5, 2016 11:12
Swift 3: how to implement a content extension that read an image attachment and intercept actions.
//
// NotificationViewController.swift
// Content
//
// Created by Barbara Rodeker on 28/09/16.
// Copyright © 2016 Barbara Rodeker. All rights reserved.
//
import UIKit
import UserNotifications
//
// CoreDataStack.swift
// CoreDataShowcase
//
// Created by Barbara Rodeker on 31.10.17.
//
// THIS CLASS CONTAINS ALL THE REQUIRED PROPERTIES THAT NEED TO BE CREATED
// FOR CORE DATA AFTER NSPersistentContainer was added in IOS 10
//
// SWIFT 4
//
// CoreDataStackTheOldWay.swift
// CoreDataShowcase
//
// Created by Barbara Rodeker on 24.09.17.
//
// THIS CLASS CONTAINS PROPERTIES THAT NEEDED TO BE
// CREATED FOR CORE DATA BEFORE THE EXISTENCE OF NSPERSISTENTCONTAINER (added in iOS10)
//
// SWIFT 4
// example to show how to check if core data can infers a mapping model by itself
// do not forget to add the .momd to you model object package!!!!
// https://developer.apple.com/documentation/coredata/nsmappingmodel/1506468-inferredmappingmodelforsourcemod
// SWIFT 4
if let previousModelURL = Bundle.main.url(forResource: "YOURMODELNAME.momd/YOURMODELVERSION 2", withExtension: "mom"),
let previousModel = NSManagedObjectModel(contentsOf: previousModelURL),
let currentModelURL = Bundle.main.url(forResource: "YOURMODELNAME.momd/YOURMODELVERSION 3", withExtension: "mom"),
let currentModel = NSManagedObjectModel(contentsOf: currentModelURL) {
// Create a persistent container with an specific description for the stores
private(set) lazy var container: NSPersistentContainer = {
let description = NSPersistentStoreDescription()
description.type = NSSQLiteStoreType
description.shouldInferMappingModelAutomatically = false
description.shouldMigrateStoreAutomatically = true
let container = NSPersistentContainer(name: CoreDataStackTheNewWay.modelName)
container.persistentStoreDescriptions = [description]
import Foundation
// Extension on PropertyListSerialization to read an array from a property list file
extension PropertyListSerialization {
// Takes the name of the PLIST file to be read. Name must be provided without extension.
// returns an optional array if the file can be opened and parsed properly.
// - Parameters:
// - named String with the filename. No extension .plist needed
static func arrayFromPlist(named name: String) -> [Any]? {