Skip to content

Instantly share code, notes, and snippets.

View gonzalezreal's full-sized avatar

Guille Gonzalez gonzalezreal

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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
Pod::Spec.new do |s|
s.name = "ReactiveCocoa"
s.version = "2.0.0dev"
s.summary = "A framework for composing and transforming sequences of values."
s.homepage = "https://github.com/blog/1107-reactivecocoa-is-now-open-source"
s.author = { "Josh Abernathy" => "josh@github.com" }
s.source = { :git => "https://github.com/ReactiveCocoa/ReactiveCocoa.git", :branch => '2.0-development' }
s.license = 'Simplified BSD License'
s.description = "ReactiveCocoa offers:\n" \
"1. The ability to compose operations on future data.\n" \