Skip to content

Instantly share code, notes, and snippets.

View gonzalezreal's full-sized avatar

Guille Gonzalez gonzalezreal

View GitHub Profile
@gonzalezreal
gonzalezreal / KeyedDecodingContainerURL.swift
Last active July 13, 2022 11:14
A simple technique to trim whitespace from URLs before decoding them with Swift Codable.
public extension KeyedDecodingContainer {
func decode(_ type: URL.Type, forKey key: KeyedDecodingContainer.Key) throws -> URL {
let stringValue = try decode(String.self, forKey: key)
guard let url = URL(string: stringValue.trimmingWhiteSpace) else {
throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "Invalid URL string.")
}
return url
}
func decodeIfPresent(_ type: URL.Type, forKey key: KeyedDecodingContainer.Key) throws -> URL? {
@gonzalezreal
gonzalezreal / IndeterminateTypesWithCodable.swift
Created April 30, 2018 19:09
Indeterminate Types with Codable in Swift
import Foundation
struct ImageAttachment: Codable {
let url: URL
let width: Int
let height: Int
}
struct AudioAttachment: Codable {
let title: String
@gonzalezreal
gonzalezreal / KeyedDecodingContainer+EmptyRepresentable.swift
Last active January 11, 2023 08:25
A technique to avoid having optional array properties in your models when decoding JSON using Swift 4
/// A type that has an "empty" representation.
public protocol EmptyRepresentable {
static func empty() -> Self
}
extension Array: EmptyRepresentable {
public static func empty() -> Array<Element> {
return Array()
}
}
@gonzalezreal
gonzalezreal / gist:8736ef16515843a40516f92cbab42702
Created February 8, 2017 13:58
Replace spaces by tabs in all swift files
find . -name "*.swift" | while read line; do unexpand -t 4 $line > $line.new; mv $line.new $line; done
@gonzalezreal
gonzalezreal / A.LICENSE.md
Last active January 26, 2023 19:43
Using Realm with Value Types

Copyright 2017 Guillermo Gonzalez

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE U

@gonzalezreal
gonzalezreal / SearchResultsViewController.swift
Created June 12, 2016 19:27
RxSwift slides – Code for the search sample
class SearchResultsViewController: UITableViewController {
// MARK: - Properties
private let viewModel: SearchResultsViewModelType
private let disposeBag = DisposeBag()
// MARK: - Initialization
init(viewModel: SearchResultsViewModelType = SearchResultsViewModel()) {
@gonzalezreal
gonzalezreal / ios-cell-registration-swift.md
Last active March 13, 2024 15:18
iOS Cell Registration & Reusing with Swift Protocol Extensions and Generics

iOS Cell Registration & Reusing with Swift Protocol Extensions and Generics

A common task when developing iOS apps is to register custom cell subclasses for both UITableView and UICollectionView. Well, that is if you don’t use Storyboards, of course.

Both UITableView and UICollectionView offer a similar API to register custom cell classes:

public func registerClass(cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String)
public func registerNib(nib: UINib?, forCellWithReuseIdentifier identifier: String)
@gonzalezreal
gonzalezreal / ValueTransformer.swift
Last active February 8, 2017 16:22
Type safe NSValueTransformer
import Foundation
private class ValueTransformer: NSValueTransformer {
let transform: (AnyObject?) -> (AnyObject?)
init(transform: (AnyObject?) -> (AnyObject?)) {
self.transform = transform
}
// MARK: NSValueTransformer
@gonzalezreal
gonzalezreal / Singleton.swift
Last active February 1, 2018 09:23
A naive? attempt to create a Singleton in Swift
class FileManager {
struct StaticInstance {
static var instance = FileManager()
}
class var defaultManager : FileManager {
return StaticInstance.instance
}
}
@gonzalezreal
gonzalezreal / TGRAsyncTestHelper.m
Last active August 29, 2015 13:58
TGRAsyncTestHelper
#import <Foundation/Foundation.h>
#define TGR_RUNLOOP_INTERVAL 0.05
#define TGR_TIMEOUT_INTERVAL 1.0
#define TGR_RUNLOOP_COUNT TGR_TIMEOUT_INTERVAL / TGR_RUNLOOP_INTERVAL
#define TGR_CAT(x, y) x ## y
#define TGR_TOKCAT(x, y) TGR_CAT(x, y)
#define __runLoopCount TGR_TOKCAT(__runLoopCount,__LINE__)