Skip to content

Instantly share code, notes, and snippets.

View alexj70's full-sized avatar

Alexandr alexj70

  • IdeaSoft
  • Ukraine
View GitHub Profile
enum NetworkResponse {
case response(URLResponse, Data)
case error(Error)
}
func processRequestResponse(_ response: NetworkResponse) {
guard
case let .response(urlResp, data) = response,
let httpResp = urlResp as? HTTPURLResponse,
200..<300 ~= httpResp.statusCode
@alexj70
alexj70 / gist:160b1693d09fb0e896cc7ac578dd738c
Created April 22, 2017 19:52
if case let, if case let where
//////////////////////////////////
enum Media {
case book(title: String, author: String, year: Int)
case movie(title: String, director: String, year: Int)
case website(urlString: String)
}
let m = Media.movie(title: "Captain America: Civil War", director: "Russo Brothers", year: 2016)
if case let Media.movie(title, _, _) = m {
@alexj70
alexj70 / gist:15d1fce423030bd14add78a666585a70
Last active April 22, 2017 19:51
Range in if statement
//////////////////////////////////
if case 200 ... 299 = statusCode {
// do something
}
@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 / 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 {

UIImage for Assets

import UIKit.UIImage
public enum Asset: String, Iteratable {
    case iconBack = "Icon-Back"
    
    var image: UIImage {
        return UIImage(asset: self)
    }
}
@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

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 / 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?) -&gt; Self {

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