Skip to content

Instantly share code, notes, and snippets.

View andr3a88's full-sized avatar

Andrea Stevanato andr3a88

View GitHub Profile
@andr3a88
andr3a88 / UINavigationControllerTransitions.swift
Created November 7, 2018 11:17
Extension for UINavigationController for custom transitions
extension UINavigationController {
/// Push the view controller with present animation
///
/// - Parameters:
/// - viewController: The view controller
/// - animationDuration: The animation duration (default 0.25)
func pushPresentViewController(viewController: UIViewController, duration: CFTimeInterval = 0.25) {
let transition = CATransition()
transition.duration = duration
@andr3a88
andr3a88 / SwitchRoutes.swift
Last active April 13, 2018 10:57
Switch route on tap like Google Maps
class RoutesViewController {
/// ...
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
if let index = self.shouldSelectPath(mapView, coordinate: coordinate, routes: results, currentRouteIndex: routeIndex) {
routeIndex = index
updateRoute()
}
}
var test = [1, 2, 3]
var n = 5
let limit = n > test.count ? test.count : n
var test2 = test[0..<limit]
var test3 = test.prefix(n)
@andr3a88
andr3a88 / .swiftlint.yml
Last active March 9, 2020 10:41
SwiftLint file
disabled_rules: # rule identifiers to exclude from running
- force_cast
- legacy_constant
- legacy_constructor
- nesting
- trailing_whitespace
- type_name
- identifier_name
- cyclomatic_complexity
- explicit_self
@andr3a88
andr3a88 / url-extensions.swift
Created February 1, 2018 16:59
URL extension to pick the parameter from url
import UIKit
extension URL {
func getValueForQueryParameter(name: String) -> String? {
let urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true)
return urlComponents?.queryItems?.first(where: { (item) -> Bool in
item.name == name
})?.value
}
@andr3a88
andr3a88 / childViewController.swift
Created January 29, 2018 09:04
Extension: Add and remove child view controller
/// Add a child view controller
///
/// - Parameter child: The child view controller
func add(_ child: UIViewController) {
addChildViewController(child)
view.addSubview(child.view)
child.didMove(toParentViewController: self)
}
@andr3a88
andr3a88 / ReuseIdentifying.swift
Created December 12, 2017 15:32
ReuseIdentifying Protocol
protocol ReuseIdentifying {
static var reuseIdentifier: String { get }
}
extension ReuseIdentifying {
static var reuseIdentifier: String {
return String(describing: Self.self)
}
}
@andr3a88
andr3a88 / Storyboard.swift
Last active September 19, 2017 09:08
Simple storyboard initialization. Require storyboard id equal to class name
enum Storyboard: String {
case menu = "Menu"
case login = "Login"
case profile = "Profile"
case map = "Map"
case settings = "Settings"
public func instantiate<VC: UIViewController>(_ viewController: VC.Type) -> VC {
guard let vc = UIStoryboard(name: self.rawValue, bundle: nil).instantiateViewController(withIdentifier: VC.storyboardIdentifier) as? VC
else { fatalError("Couldn't instantiate \(VC.storyboardIdentifier) from \(self.rawValue)") }
import Foundation
import RealmSwift
protocol CascadingDeletable {
var cascadingDeletions: [AnyObject?] { get }
}
extension Realm {
func delete<T: AnyObject>(_ objects: List<T>, cascade: Bool = true) where T: CascadingDeletable {