Skip to content

Instantly share code, notes, and snippets.

View andr3a88's full-sized avatar

Andrea Stevanato andr3a88

View GitHub Profile
@andr3a88
andr3a88 / guard.swift
Last active November 2, 2015 11:49
Swift 2.0 Guard Statement
// Like an if statement, guard executes statements based on a Boolean value of an expression.
// Unlike an if statement, guard statements only run if the conditions are not met.
// You can think of guard more like an Assert, but rather than crashing, you can gracefully exit.
func fooGuardNonOptional(x: Int) {
guard x > 0 else {
// Value requirements not met, do something
return
}
@andr3a88
andr3a88 / loopLabels.swift
Last active January 26, 2016 11:49
Swift Loop Labels
sectionLoop: for section in sections {
rowLoop: for row in rows {
if row.isMagical {
break sectionLoop
}
}
}
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 {
@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)") }
@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 / 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 / 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 / .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
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 / 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()
}
}