Skip to content

Instantly share code, notes, and snippets.

Avatar

Sash Zats zats

View GitHub Profile
@zats
zats / usage.swift
Created Jul 16, 2016
Optional escape operator
View usage.swift
// flow controll using do-catch
do {
try commentBody(¿commentRangeBeforeOffset(¿getNameOffset(dictionary)))
} catch let e as OptionalError {
} catch {
}
// folding into optional value. Currently produces double optional
@zats
zats / pubsub.swift
Last active May 26, 2016
PubSub protocol
View pubsub.swift
protocol PubSubMessageType {
associatedtype Topic
var topic: Topic { get }
}
protocol PubSubType {
associatedtype Message = PubSubMessageType
associatedtype Topic
@zats
zats / Problem.swift
Last active Oct 28, 2020
Recursive structures in Swift without Box classes
View Problem.swift
struct Node<T> { // error: recursive value type 'Node<T>' is not allowed
var child: Node?
var value: T
init(value: T, child: Node? = nil) {
self.value = value
self.child = child
}
}
View NoMoreCycle.swift
class Parent {
var child: Child? {
didSet {
child.callback = weak(self)?.delegateCallback
}
}
func delegateCallback() {
// ...
@zats
zats / retain-cycle.swift
Last active Jan 4, 2016
Delegate-pattern without a weak delegate object creates retain cycle
View retain-cycle.swift
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() {
// ...
}
@zats
zats / README.md
Last active Jul 2, 2019
UIPreviewActionItem for SFSafariViewController through delegation not subclassing
View README.md

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 / UIViewController.m
Last active Oct 5, 2015
UIViewController implementation details for view and loadViewIfNeeded
View UIViewController.m
- (UIView *)view {
[self loadViewIfRequired];
return _existingView;
}
- (void)loadViewIfNeeded {
[self loadViewIfRequired];
}
@zats
zats / script.swift
Last active Mar 5, 2021
Update all your plugins for the latest Xcode beta with a single
View script.swift
#!/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 / dictionary_diff.swift
Created Aug 20, 2015
Abandoned dictionary diffing in swift
View dictionary_diff.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 / Output
Last active Aug 29, 2015
Fizz Buzz with Gameplay Kit in Swift
View Output
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz