Skip to content

Instantly share code, notes, and snippets.

View haashem's full-sized avatar
🏠
Working from home

Hashem haashem

🏠
Working from home
View GitHub Profile
@rockbruno
rockbruno / immutable.swift
Created April 2, 2020 12:10
Modifying immutable structs without var in Swift
protocol ImmutableCopy {
associatedtype InitializerTuple
var lastInitParams: AnyInitializerParams<InitializerTuple> { get }
func copy(from: (InitializerTuple) -> Self, applying: (inout InitializerTuple) -> Void) -> Self
}
extension ImmutableCopy {
func copy(from: (InitializerTuple) -> Self, applying: (inout InitializerTuple) -> Void) -> Self {
var values = lastInitParams.values
applying(&values)
@danielctull
danielctull / UICollectionViewFlowLayout+DelegateAccess.swift
Last active May 16, 2019 14:00
Wraps the six UICollectionViewFlowLayout delegate methods and their equivalent properties using functional programming techniques, so that values are easier to retrieve. Full details at: http://danieltull.co.uk/blog/2018/04/13/simplifying-uicollectionviewflowlayout-delegate-method-usage-with-functional-programming/
extension UICollectionViewFlowLayout {
typealias DelegateMethod<Key, Value> = ((UICollectionView, UICollectionViewLayout, Key) -> Value)
private var delegate: UICollectionViewDelegateFlowLayout? {
return collectionView?.delegate as? UICollectionViewDelegateFlowLayout
}
func retrieve<Key, Value>(
import FirebaseFirestore
private struct Property {
let label: String
let value: Any
}
struct FirestoreModelData {
let snapshot: DocumentSnapshot
@Juanpe
Juanpe / String+Localization.swift
Last active July 31, 2023 11:42
String extension to localize more easy way
extension String {
var localized: String {
return NSLocalizedString(self, comment: "\(self)_comment")
}
func localized(_ args: [CVarArg]) -> String {
return localized(args)
}
@eMdOS
eMdOS / Codable.swift
Last active April 23, 2019 15:04
Codable {Apple Swift version 4.0 (swiftlang-900.0.45.6 clang-900.0.26)}
extension Encodable {
func encode(with encoder: JSONEncoder = JSONEncoder()) throws -> Data {
return try encoder.encode(self)
}
}
extension Decodable {
static func decode(with decoder: JSONDecoder = JSONDecoder(), from data: Data) throws -> Self {
return try decoder.decode(Self.self, from: data)
}
@ilyapuchka
ilyapuchka / UIVisualEffect.swift
Last active January 21, 2020 21:57
(Ab)using UIVisualEffectView effect settings
extension UIVisualEffectView {
private var filterLayer: CALayer? {
return layer.sublayers?.first
}
private var blurFilter: NSObject? {
return filterLayer?
.filters?.flatMap({ $0 as? NSObject })
.first(where: { $0.value(forKey: "name") as? String == "gaussianBlur" })
@ollieatkinson
ollieatkinson / HTTPStatusCode.swift
Last active April 15, 2024 18:34
HTTP status codes as a Swift enum.
/// This is a list of Hypertext Transfer Protocol (HTTP) response status codes.
/// It includes codes from IETF internet standards, other IETF RFCs, other specifications, and some additional commonly used codes.
/// The first digit of the status code specifies one of five classes of response; an HTTP client must recognise these five classes at a minimum.
enum HTTPStatusCode: Int, Error {
/// The response class representation of status codes, these get grouped by their first digit.
enum ResponseType {
/// - informational: This class of status code indicates a provisional response, consisting only of the Status-Line and optional headers, and is terminated by an empty line.
case informational
@alexanderkhitev
alexanderkhitev / gist:93bee949907d4c482bb8d413800daeda
Last active December 4, 2017 09:23
isValidEmail string extension for swift 3
var isValidEmail: Bool {
do {
let regex = try NSRegularExpression(pattern: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}", options: .caseInsensitive)
return (regex.firstMatch(in: self, options: [], range: NSMakeRange(0, self.characters.count)) != nil)
} catch {
debugPrint(error.localizedDescription)
return false
}
}
@LuizZak
LuizZak / retry.swift
Last active February 12, 2018 10:27
PromiseKit retrying functions using encapsulated blocks that return promises
/// Retries the given throwing `block` as a promise `tries` times,
/// re-calling `block` another time until exhausting the tries.
func retry<T>(tries count: Int, block: () throws -> T) -> Promise<T> {
return Promise().thenRetry(tries: count, block: block)
}
/// Retries the given promise-returning `block` as a promise `tries` times,
/// re-calling `block` another time until exhausting the tries.
func retry<T>(tries count: Int, block: () -> Promise<T>) -> Promise<T> {
return Promise().thenRetry(tries: count, block: block)
@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)