Skip to content

Instantly share code, notes, and snippets.

View alexj70's full-sized avatar

Alexandr alexj70

  • IdeaSoft
  • Ukraine
View GitHub Profile
@alexj70
alexj70 / SubclassingStructs.md
Last active April 26, 2017 20:54
“Subclassing" Structs

“Subclassing" Structs with Enum

enum PostType {
    case text(text: String)
    case image(image: UIImage, legend: String)
    case video(url: URL, duration: TimeInterval)
}

struct Post {
 let date: Date
@alexj70
alexj70 / Formatters.swift
Last active April 22, 2017 19:13
Cached formatter
import Foundation
import Contacts
import MapKit
// MARK: - Distatnce formatter
fileprivate let _distanceFormatter: MKDistanceFormatter = {
let formatter = MKDistanceFormatter()
return formatter
}()

Guard self

guard let `self` = self else { return }

Using

UIView.animate(withDuration: 0.25) {
	[weak self] in
	guard let `self` = self else { return }
	self.isHidden = true
@alexj70
alexj70 / NibLoadableView.md
Last active April 26, 2017 20:52
Nib loaded

NibLoaded View

import UIKit
protocol NibLoadableView: class {}
extension NibLoadableView where Self: UIView {
    static func fromNib() -> Self {
        return fromNib(nibName: nil)
    }
    
 static func fromNib(nibName: String?) -> Self {

Storyboard segue

import UIKit
protocol SegueHandler {
    associatedtype SegueIdentifier: RawRepresentable
}

extension SegueHandler where Self: UIViewController, SegueIdentifier.RawValue == String {
    
    func performSegue(withIdentifier identifier: SegueIdentifier, sender: Any?) {
@alexj70
alexj70 / UIStoryboard.swift
Last active June 7, 2021 12:10
UIStoryboard extension
import UIKit
extension UIStoryboard {
/// Main storyboard
public var main: UIStoryboard {
return UIStoryboard(name: "Main", bundle: nil)
}
/// Instantiates and returns the view controller with the specified identifier.
///
/// - Parameter identifier: uniquely identifies equals to Class name

UIImage for Assets

import UIKit.UIImage
public enum Asset: String, Iteratable {
    case iconBack = "Icon-Back"
    
    var image: UIImage {
        return UIImage(asset: self)
    }
}
@alexj70
alexj70 / UIColor.md
Last active April 27, 2017 21:28
UIColor from hex

UIColor

enum ColorType: UInt32 {
    case tint = 0x005054ff
    case background = 0xF5F5F2ff
   
   var color: UIColor { return UIColor(type: self) }
}

extension UIColor {
@alexj70
alexj70 / Font.md
Last active April 25, 2017 17:23
Font

UIFont

import UIKit.UIFont
protocol FontConvertible {
    func font(size: CGFloat) -> UIFont
}

extension FontConvertible where Self: RawRepresentable, Self.RawValue == String {
    func font(size: CGFloat) -> UIFont {
        return UIFont(font: self, size: size)
@alexj70
alexj70 / gist:15d1fce423030bd14add78a666585a70
Last active April 22, 2017 19:51
Range in if statement
//////////////////////////////////
if case 200 ... 299 = statusCode {
// do something
}