Skip to content

Instantly share code, notes, and snippets.

View zats's full-sized avatar

Sash Zats zats

View GitHub Profile
@zats
zats / README.md
Last active July 11, 2021 16:06
Jeeves configuration sample
  • routes – an array of route objects
    • request - configuration of a request.
      • method – a string according to RFC 2616.
      • pattern - a string to match, can be an escaped regex string. If regex is specified, it must match the entire request path.
    • response - configuration of the response.
      • resourcePath - relative path of the resource to serve, if the request was matched.
      • contentType - optional an override for the resource content type. By default it's automatically deduced from the file MIME type.
@zats
zats / WMLViewDebugging.m
Last active June 28, 2016 01:46
Improving View Debugging in Xcode by showing ViewController class this view belongs to http://blog.zats.io/2015/06/16/improving-view-debugging-in-xcode/
#ifdef DEBUG
#import "WMLSwizzler.h"
static SEL wml_loadViewSEL;
static void wml_swizzleLoadViewForClass(Class class) {
typedef void(*load_view_t)(id, SEL);
__block load_view_t loadView = (load_view_t)[class S_replaceInstanceMethod:wml_loadViewSEL withBlock:^(UIViewController *self){
loadView(self, wml_loadViewSEL);
@zats
zats / gist:e1524d10e6bd35f15088
Last active August 29, 2015 14:23
Don't let me down
let str: String
dispatch_sync(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)) { // Variable 'str' used before being initialized
str = "Hello"
}
print(str) // Variable 'str' used before being initialized
@zats
zats / random.swift
Last active August 29, 2015 14:27
GKRandomDistribution vs GKShuffledDistribution
import Cocoa
import GameplayKit
let randoms = NSCountedSet()
let shuffles = NSCountedSet()
let gausians = NSCountedSet()
let randomD3 = GKRandomDistribution(lowestValue: 1, highestValue: 3)
let shuffledD3 = GKShuffledDistribution(lowestValue: 1, highestValue: 3)
let gausianD3 = GKGaussianDistribution(lowestValue: 1, highestValue: 3)
@zats
zats / Output
Last active August 29, 2015 14:27
Fizz Buzz with Gameplay Kit in Swift
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
@zats
zats / dictionary_diff.swift
Created August 20, 2015 20:53
Abandoned dictionary diffing in swift
import Foundation
extension Dictionary {
static func keyDifference<Value: Equatable>(dictionary1 dic1: Dictionary<Key, Value>, dictionary2 dic2: Dictionary<Key, Value>, inout inserted: Set<Key>, inout deleted: Set<Key>, inout updated:Set<Key>, inout unchanged: Set<Key>) {
let keys1 = Set(dic1.keys.array)
let keys2 = Set(dic2.keys.array)
let allKeys = keys1.union(keys2)
inserted = []
@zats
zats / script.swift
Last active March 5, 2021 01:32
Update all your plugins for the latest Xcode beta with a single
#!/usr/bin/env xcrun swift
// $ chmod +x script.swift
// $ ./script.swift
// or $ ./script.swift -xcode=/Applications/Xcode-beta.app
import Foundation
@noreturn private func failWithError(message: String) {
print("🚫 \(message)")
@zats
zats / UIViewController.m
Last active October 5, 2015 02:35
UIViewController implementation details for view and loadViewIfNeeded
- (UIView *)view {
[self loadViewIfRequired];
return _existingView;
}
- (void)loadViewIfNeeded {
[self loadViewIfRequired];
}
@zats
zats / README.md
Last active July 2, 2019 10:45
UIPreviewActionItem for SFSafariViewController through delegation not subclassing

Adding UIPreviewActionItem to SFSafariViewController might be a tedious task. This extansion should help.

let vc = SFSafariViewController(initialURL: url, entersReaderIfAvailable: true)
vc.previewActionItemsDelegate = self

When presenting SFSafariViewController use convenience initializer that will store original URL for later, it'll make custom action requiring original URL easier. But it's not mandatory.

Here is the delegate implementation, this is where we might want to use initialURL

func safariViewControllerPreviewActionItems(controller: SFSafariViewController) -> [UIPreviewActionItem] {
@zats
zats / retain-cycle.swift
Last active January 4, 2016 18:02
Delegate-pattern without a weak delegate object creates retain cycle
class Parent {
var child: Child? {
didSet {
child.callback = delegateCallback // Fix: child.callback = weakify(self, Parent.delegateCallback) using https://github.com/klundberg/Weakify/blob/master/Weakify/Weakify.swift#L15-L21
}
}
func delegateCallback() {
// ...
}