Skip to content

Instantly share code, notes, and snippets.

View danielt1263's full-sized avatar

Daniel Tartaglia danielt1263

View GitHub Profile
//
// UIColorExtensions.swift
//
// Created by Daniel Tartaglia on 12/4/14.
// Copyright (c) 2014 Daniel Tartaglia. MIT License.
//
import UIKit
@danielt1263
danielt1263 / DictionaryExtensions.swift
Last active May 27, 2017 19:53
Swift Extensions... These are extensions to Swift standard types that I find useful.
//
// DictionaryExtensions.swift
//
// Created by Daniel Tartaglia on 6/18/16.
// Copyright © 2016 Daniel Tartaglia. MIT License.
//
extension Dictionary {
@danielt1263
danielt1263 / UITextField+Extensions.swift
Last active July 20, 2017 19:28
Allows you to set the text in a UITextField *without* changing the position of the cursor.
//
// UITextField+Extensions.swift
//
// Created by Daniel Tartaglia on 8/8/16.
// Copyright © 2016 MIT License.
//
import UIKit

Two Level Type Erasing in Swift 3

Recently I converted a project of mine to Swift 3 (https://github.com/dtartaglia/XStreamSwift) and I had to deal with a problem that I couldn't find an answer for. This article is about the problem and the solution I discovered.

There are lots of articles on the web about type erasing in Swift, but all the ones I found only dealt with a single level. I will recap the concept quickly:

protocol Listener
{

associatedtype ListenerValue

@danielt1263
danielt1263 / AppStore.swift
Last active January 10, 2017 20:56
App Store implementation
//
// AppStore.swift
//
// Created by Daniel Tartaglia on 1/10/17.
// Copyright © 2017 Daniel Tartaglia. MIT License.
//
import Foundation
import StoreKit
//
// UIImage+Extensions.swift
//
// Created by Daniel Tartaglia on 4/25/16.
// Copyright © Daniel Tartaglia. MIT License.
//
import UIKit
//
// Dispatcher.swift
// Flux
//
// Created by Daniel Tartaglia on 3/13/17.
// Copyright © 2017 Daniel Tartaglia. MIT License.
//
import Foundation
@danielt1263
danielt1263 / Store.swift
Last active November 9, 2020 18:02
A stripped down version of The Elm Architecture for Swift. Great for implementing state machines.
//
// Store.swift
//
// Created by Daniel Tartaglia on 3/11/17.
// Copyright © 2020 Daniel Tartaglia. MIT License
//
import Foundation
import RxSwift
@danielt1263
danielt1263 / encapsulating user code_1.swift
Last active April 12, 2017 18:49
Gists supporting the "Encapsulating the User" blog post.
//
// ViewController.swift
//
// Created by Daniel Tartaglia on 1/27/17.
// Copyright © 2017 Daniel Tartaglia. MIT License.
//
import UIKit
class ViewController: UIViewController
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = (info[UIImagePickerControllerEditedImage] as? UIImage) ?? (info[UIImagePickerControllerOriginalImage] as? UIImage)
if let data = image.flatMap({ UIImageJPEGRepresentation($0, 0.8)} ) {
api.upload(avatar: data).then { [weak self] in
self?.avatarView.image = image
self?.dismiss(animated: true, completion: nil)
}
.catch { [weak self] error in