Skip to content

Instantly share code, notes, and snippets.

Avatar

Alexander Momchilov amomchilov

View GitHub Profile
View ExtratcUnkeyContainerToArray.swift
import Foundation
struct S<T> {
let elements: [T]
}
extension S: Decodable where T: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.unkeyedContainer()
let s = sequence(state: container, next: {
View PointerIssue.swift
import Dispatch
import Foundation
// The Weak struct is the weak wrapper
struct Weak<T: AnyObject> {
weak var object: T?
}
// Stand-in for AUGraphAddRenderNotify
func call(
@amomchilov
amomchilov / PrivacyViolator.rb
Last active Mar 4, 2020
Temporarily by-pass access modifiers on an object.
View PrivacyViolator.rb
#!/usr/bin/ruby
def disrespect_privacy(symbol, &block) # access private methods in a block
raise ArgumentError, 'Block must be specified' unless block_given?
block_binding = block.binding
target_object = block_binding.local_variable_get(symbol)
block_binding.local_variable_set(symbol, PrivacyViolator.new(target_object))
yield
View RoundedAnnulusSector.swift
import UIKit
import PlaygroundSupport
class ArcView: UIView {
private var strokeWidth: CGFloat {
return CGFloat(min(self.bounds.width, self.bounds.height) * 0.25)
}
override open func draw(_ rect: CGRect) {
super.draw(rect)
@amomchilov
amomchilov / DownloadProgressIndicatorDemo.swift
Last active Aug 23, 2019 — forked from mminer/DownloadProgressIndicatorDemo.swift
Displays a progress indicator on a file in the Finder.
View DownloadProgressIndicatorDemo.swift
import Cocoa
let path = "/Users/You/Pick/Any/Random/File/On/Your/System.txt"
let destination = URL(fileURLWithPath: path)
let progress: Progress = {
let p = Progress(parent: nil, userInfo: [
.fileOperationKindKey: Progress.FileOperationKind.downloading,
.fileURLKey: destination,
])
@amomchilov
amomchilov / Download Progress Example.swift
Last active Jun 5, 2019
An example of using (NS)Progress to show a progress bar in Finder
View Download Progress Example.swift
import Cocoa
let path = "/Users/You/Pick/Any/Random/File/On/Your/System.txt"
let destination = URL(fileURLWithPath: path)
let progress: Progress = {
let p = Progress(parent: nil, userInfo: [
.fileOperationKindKey: Progress.FileOperationKind.downloading,
.fileURLKey: destination,
])
View mostFrequentWord.swift
extension String {
func mostFrequentWord() -> String? {
return Dictionary(
grouping: self
.trimmingCharacters(in: CharacterSet.letters)
.lowercased()
.components(separatedBy: CharacterSet.whitespacesAndNewlines),
by: { $0 }
)
.max(by:) { $0.value.count < $1.value.count }?.key // `key` is word, `value` is array of word, repeated `count` times
View Improvements.swift
// 1. Don't compare to `nil`, only to force unwrap later. Just use conditional binding:
func allOfferCards() -> [OfferCard]{
guard let dataSource = dataSource else { return [] }
let numberOfCards = self.dataSource.kolodaNumberOfCards(self)
var offerCards = [OfferCard]()
for i in 0..<numberOfCards {
@amomchilov
amomchilov / Gif stitcher
Last active Jan 15, 2017
Vertically stitches frames of a GIF into a single image
View Gif stitcher
let image = #imageLiteral(resourceName: "Arrow.gif")
let imageRep = image.representations.first! as! NSBitmapImageRep
let numFrames = imageRep.value(forProperty: NSImageFrameCount) as! Int
let numLoops = imageRep.value(forProperty: NSImageCurrentFrameDuration) as! Float
var cgImages = [CGImage]()
var nsImages = [NSImage]()
var size = NSSize()
for frame in 0..<numFrames {
You can’t perform that action at this time.