Skip to content

Instantly share code, notes, and snippets.

View rockbruno's full-sized avatar

Bruno Rocha rockbruno

View GitHub Profile
@rockbruno
rockbruno / Heap.swift
Created October 8, 2020 17:19
PriorityQueue
final class Heap<T> {
typealias Comparator = (T,T) -> Bool
var elements: [T]
let priority: Comparator
init(elements: [T], priority: @escaping Comparator) {
self.priority = priority
self.elements = elements
@rockbruno
rockbruno / invisibleCharacter.swift
Created September 17, 2020 12:21
Invisible Xcode characters
// These little spaces are invisible in Xcode. Copy them around and watch everything break!
func foo(arg: Int) {
print("\(arg)")
}
@rockbruno
rockbruno / immutable.swift
Created April 2, 2020 12:10
Modifying immutable structs without var in Swift
protocol ImmutableCopy {
associatedtype InitializerTuple
var lastInitParams: AnyInitializerParams<InitializerTuple> { get }
func copy(from: (InitializerTuple) -> Self, applying: (inout InitializerTuple) -> Void) -> Self
}
extension ImmutableCopy {
func copy(from: (InitializerTuple) -> Self, applying: (inout InitializerTuple) -> Void) -> Self {
var values = lastInitParams.values
applying(&values)
@rockbruno
rockbruno / buck_targets_to_test.swift
Last active October 14, 2019 23:50
Buck: Script to find out which rules should be tested based on what was changed
import Foundation
func run(command: String, wait: Bool = true) -> String? {
let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["-c", command]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
guard wait else {
@rockbruno
rockbruno / ios_header_remover.swift
Last active September 8, 2021 09:26
iOS File Header remover script
import Foundation
func findFiles(rootPath: String, suffix: String, ignoreDirs: Bool = true) -> [String]? {
var result = [String]()
let fileManager = FileManager.default
if let paths = fileManager.subpaths(atPath: rootPath) {
let swiftPaths = paths.filter { $0.hasSuffix(suffix) }
for path in swiftPaths {
var isDir : ObjCBool = false
let fullPath = (rootPath as NSString).appendingPathComponent(path)
<html>
<head>
<title>Take Picture</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="apple-touch-icon-precomposed" href="http:/localhost:58844/webclips/images/Take%2520Picture/icon.png">
<link rel="apple-touch-startup-image" media="(orientation: landscape)" href="http:/localhost:58844/webclips/images/Take%2520Picture/landscape-launch.png"/>
<link rel="apple-touch-startup-image" media="(orientation: portrait)" href="http:/localhost:58844/webclips/images/Take%2520Picture/portrait-launch.png"/>
@rockbruno
rockbruno / DerivedConformanceCaseIterable+first.cpp
Last active September 21, 2018 19:09
CaseIterable + first property
ValueDecl *DerivedConformance::deriveCaseIterable(ValueDecl *requirement) {
// Conformance can't be synthesized in an extension.
if (checkAndDiagnoseDisallowedContext(requirement))
return nullptr;
// Check that we can actually derive CaseIterable for this type.
if (!canDeriveConformance(Nominal))
return nullptr;
Type returnTy;
class CustomViewController<CustomView: UIView>: UIViewController {
var customView: CustomView {
return view as! CustomView
}
override func loadView() {
view = CustomView()
}
}
final class MyViewController: UIViewController, HasCustomView {
typealias CustomView = MyView
override func loadView() {
let customView = CustomView()
customView.delegate = self
view = customView
}
override func viewDidLoad() {
/// The HasCustomView protocol defines a customView property for UIViewControllers to be used in exchange of the regular view property.
/// In order for this to work, you have to provide a custom view to your UIViewController at the loadView() method.
public protocol HasCustomView {
associatedtype CustomView: UIView
}
extension HasCustomView where Self: UIViewController {
/// The UIViewController's custom view.
public var customView: CustomView {
guard let customView = view as? CustomView else {