Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Guille Gonzalez gonzalezreal

🏠
Working from home
View GitHub Profile
@gonzalezreal
gonzalezreal / KeyedDecodingContainerURL.swift
Last active Aug 4, 2021
A simple technique to trim whitespace from URLs before decoding them with Swift Codable.
View KeyedDecodingContainerURL.swift
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 Apr 30, 2018
Indeterminate Types with Codable in Swift
View IndeterminateTypesWithCodable.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 Jul 30, 2021
A technique to avoid having optional array properties in your models when decoding JSON using Swift 4
View KeyedDecodingContainer+EmptyRepresentable.swift
/// 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 Feb 8, 2017
Replace spaces by tabs in all swift files
View gist:8736ef16515843a40516f92cbab42702
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 Jul 1, 2020
Using Realm with Value Types
View A.LICENSE.md

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 Jun 12, 2016
RxSwift slides – Code for the search sample
View SearchResultsViewController.swift
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 Feb 14, 2022
iOS Cell Registration & Reusing with Swift Protocol Extensions and Generics
View ios-cell-registration-swift.md

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 Feb 8, 2017
Type safe NSValueTransformer
View ValueTransformer.swift
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 Feb 1, 2018
A naive? attempt to create a Singleton in Swift
View Singleton.swift
class FileManager {
struct StaticInstance {
static var instance = FileManager()
}
class var defaultManager : FileManager {
return StaticInstance.instance
}
}
View TGRAsyncTestHelper.m
#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__)