Skip to content

Instantly share code, notes, and snippets.

View josipbernat's full-sized avatar

Josip Bernat josipbernat

View GitHub Profile
@josipbernat
josipbernat / UIView+Identifier
Created November 6, 2020 11:39
When regular Int tag isn't sufficient and you need string representation of UIView identifier
fileprivate var StringIdentifierHandle: UInt8 = 2
extension UIView {
var stringIdentifier: String? {
get {
return objc_getAssociatedObject(self, &StringIdentifierHandle) as? String
}
set {
objc_setAssociatedObject(self, &StringIdentifierHandle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
@josipbernat
josipbernat / ComparableCaseEnum.swift
Created November 4, 2020 11:49
Compare enum with associated value by ignoring their associated values
// Thanks to following StackOverflow answer:
// https://stackoverflow.com/questions/47597489/compare-swift-enum-types-ignoring-associated-values-generic-implementation
protocol ComparableCaseEnum {
func isSameCase(as other: Self) -> Bool
}
extension ComparableCaseEnum {
func isSameCase(as other: Self) -> Bool {
let mirrorSelf = Mirror(reflecting: self)
@josipbernat
josipbernat / Collection-Extensions.swift
Created November 4, 2020 11:46
Safe array access in swift let's you access index out of bounds without app crash
extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}
@josipbernat
josipbernat / EditableAssociatedValue-Enum.swift
Last active November 4, 2020 15:45
Enum with associated values that can be updated
enum PersonInfo {
// CustomDebugStringConvertible is here for easier printing to console.
// Feel free to remove it if you don't need it.
class EnumValue<T>: CustomDebugStringConvertible {
var value: T
init(_ value: T) {
self.value = value
}
@josipbernat
josipbernat / UIView-Extensions.swift
Last active October 27, 2020 13:27
Easy accessing UIView XIBs when XIB file has the same name as class
extension UIView {
//MARK: - XIBs
// https://stackoverflow.com/questions/24857986/load-a-uiview-from-nib-in-swift
public class var nibName: String {
let name = "\(self)".components(separatedBy: ".").first ?? ""
return name
}
public class var nib: UINib {
@josipbernat
josipbernat / UIStoryboard-Extensions.swift
Last active October 27, 2020 10:37
Instating view controller from storyboard in easier way
extension UIStoryboard {
func instantiateViewController<T: UIViewController>() -> T {
let viewController = self.instantiateViewController(withIdentifier: T.className)
guard let typedViewController = viewController as? T else {
fatalError("Unable to cast view controller of type (\(type(of: viewController))) to (\(T.className))")
}
return typedViewController
}
@josipbernat
josipbernat / FadeOperation.swift
Created November 3, 2017 12:07
AVAudioPlayer fade in and fade out operation
//
// FadeOperation.swift
// SomeProject
//
// Created by Josip Bernat on 03/11/2017.
// Copyright © 2017 Josip's Home. All rights reserved.
//
import Foundation
import AVFoundation