Skip to content

Instantly share code, notes, and snippets.

Avatar
🎸

Bruno Rocha rockbruno

🎸
View GitHub Profile
View Heap.swift
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 Sep 17, 2020
Invisible Xcode characters
View invisibleCharacter.swift
// 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 Apr 2, 2020
Modifying immutable structs without var in Swift
View immutable.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 Oct 14, 2019
Buck: Script to find out which rules should be tested based on what was changed
View buck_targets_to_test.swift
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 Feb 1, 2020
iOS File Header remover script
View ios_header_remover.swift
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)
View Shortcuts app decoded shortcut HTML
<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"/>
View DerivedConformanceCaseIterable+first.cpp
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;
View ViewCode7.swift
class CustomViewController<CustomView: UIView>: UIViewController {
var customView: CustomView {
return view as! CustomView
}
override func loadView() {
view = CustomView()
}
}
View ViewCode6.swift
final class MyViewController: UIViewController, HasCustomView {
typealias CustomView = MyView
override func loadView() {
let customView = CustomView()
customView.delegate = self
view = customView
}
override func viewDidLoad() {
View ViewCode5.swift
/// 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 {