Skip to content

Instantly share code, notes, and snippets.

@GemmaDelOlmo
GemmaDelOlmo / UIImage+Tint.swift
Last active September 15, 2016 06:45
Swift extension on UIImage for tinting with UIColor
extension UIImage
{
func tint(color:UIColor) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
context.scaleBy(x: 1.0, y: -1.0)
context.translateBy(x: 0.0, y: -size.height)
@GemmaDelOlmo
GemmaDelOlmo / String+EmailValidation.swift
Last active September 16, 2016 09:48
Swift extension on String for email validation
extension String {
func validateEmail() -> Bool {
let emailRegEx = "^.+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*$"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluate(with: self)
}
}
@GemmaDelOlmo
GemmaDelOlmo / UIColor+Hex.swift
Created September 19, 2016 09:16
Swift extension on UIColor for initialization with hex color
extension UIColor {
public convenience init(hex: Int) {
let components = (
R: CGFloat((hex >> 16) & 0xff) / 255,
G: CGFloat((hex >> 08) & 0xff) / 255,
B: CGFloat((hex >> 00) & 0xff) / 255
)
self.init(red: components.R, green: components.G, blue: components.B, alpha: 1)
}
}
@GemmaDelOlmo
GemmaDelOlmo / UIImageView+BackgroundImageDownload.swift
Created November 15, 2016 08:17
Swift UIImageView extension to download image from an URL in background
extension UIImageView {
func addImageFromURL(urlString: String) {
guard let imgURL: NSURL = NSURL(string: urlString) else { return }
let request: NSURLRequest = NSURLRequest(url: imgURL as URL)
NSURLConnection.sendAsynchronousRequest(
request as URLRequest, queue: OperationQueue.main,
completionHandler: {(response: URLResponse? ,data: Data? ,error: Error? ) -> Void in
if error == nil {
self.image = UIImage(data: data!)
}
@GemmaDelOlmo
GemmaDelOlmo / DictionaryToJsonString.swift
Created December 13, 2016 09:30
Example on how to convert from Dictionary to JSON string on Swift 3
import Foundation
do {
let testDictionary = ["1": "Foo",
"2": "Bar"]
let JSONData = try JSONSerialization.data(withJSONObject:
testDictionary ,
options: .prettyPrinted)
let JSONString = NSString(data: JSONData,
encoding: String.Encoding.ascii.rawValue)
@GemmaDelOlmo
GemmaDelOlmo / StrategyGuide.swift
Last active November 2, 2022 10:30
Strategy pattern guide for MVVM swift 3
//STEPS TO IMPLEMENT STRATEGY PATTERN ON PROTOCOL ORIENTED MVVM VIEWCONTROLLER WITH MVVM CELLS
//1: The ViewController will have a collection of cell viewModels
//2: There must be a drawer class for each cell that implements the cell drawer protocol
//3: Each of the cellViewModels must implement the viewModel generic protocol in order to have a reference to the cellDrawer
//DRAWER AND VIEWMODEL PROTOCOLS
protocol <#YOUR_DRAWER_PROTOCOL_NAME#> {
func cellFor(tableView: UITableView, atIndexPath: IndexPath) -> UITableViewCell
func draw(_ cell: UITableViewCell, cellViewModel: Any, viewModel: <#CONTROLLER_VIEWMODEL_PROTOCOL#>)
func heightFor(tableView: UITableView, atIndexPath: IndexPath, cellViewModel: Any) -> CGFloat
@GemmaDelOlmo
GemmaDelOlmo / Equatables.swift
Last active February 3, 2017 19:22
Playground with an example of the Equatable protocol implementation.
//: Playground - noun: a place where people can play
/*
Created by: Gemma del Olmo
*/
import UIKit
class Person {
let age : Int
let firstName : String
@GemmaDelOlmo
GemmaDelOlmo / Optional+Unwrapping.swift
Last active December 30, 2018 10:59
Optional extension for easily unwrapping one or more variables.
extension Optional {
//Executes the closure if there is some value
func then(_ handler: (Wrapped) -> Void) {
switch self {
case .some(let wrapped): return handler(wrapped)
case .none: break
}
}
@GemmaDelOlmo
GemmaDelOlmo / UITableView+Reuse.swift
Created December 1, 2017 10:44
UITableViewExtension to help reuse cells
extension UITableView {
func register<T: UITableViewCell>(_: T.Type) where T: NibLoadableView {
let bundle = Bundle(for: T.self)
let nib = UINib(nibName: T.nibName, bundle: bundle)
self.register(nib, forCellReuseIdentifier: T.defaultReuseIdentifier)
}
func registerHeader<T: UIView>(_: T.Type) where T: ReusableView, T: NibLoadableView {
@GemmaDelOlmo
GemmaDelOlmo / String+Localize.swift
Created December 1, 2017 10:45
String extension to easily localize without NSLocalizedString
extension String {
static func localized(_ identifier: String) -> String {
return NSLocalizedString(identifier, comment: "")
}
}