Skip to content

Instantly share code, notes, and snippets.

View ilyapuchka's full-sized avatar

Ilya Puchka ilyapuchka

View GitHub Profile
@ilyapuchka
ilyapuchka / UIVisualEffect.swift
Last active January 21, 2020 21:57
(Ab)using UIVisualEffectView effect settings
extension UIVisualEffectView {
private var filterLayer: CALayer? {
return layer.sublayers?.first
}
private var blurFilter: NSObject? {
return filterLayer?
.filters?.flatMap({ $0 as? NSObject })
.first(where: { $0.value(forKey: "name") as? String == "gaussianBlur" })
@ilyapuchka
ilyapuchka / StyleProxy.swift
Last active May 19, 2020 20:14
Adaptive Font styles
public protocol Stylish: class {
func updateStyle()
}
public class StyleProxy<S: Stylish>: NSObject {
fileprivate override init() { }
}
private class StyleProxyView<S: Stylish>: UIView {
@ilyapuchka
ilyapuchka / NSCodingBox.swift
Last active September 23, 2016 20:52
Swift & NSCoding
public final class Box<T> {
public let value: T
public init(value: T) {
self.value = value
}
}
public final class NSCodingBox<T: Coding>: NSObject, NSCoding {
public let value: T
@ilyapuchka
ilyapuchka / gist:6ef4a708fb438c35e66b165130b1f20e
Last active October 14, 2017 08:35
Generic typealias in protocol
/*
Xcode 8 beta 6
Apple Swift version 3.0 (swiftlang-800.0.43.6 clang-800.0.38)
Target: x86_64-apple-macosx10.9
*/
struct Generic<A, B> {}
//if typealias is defined inside the concrete type it works:
class SomeClass {
@ilyapuchka
ilyapuchka / CollectionType+Next
Created January 1, 2016 16:02
Conveniently access next element in collection
extension CollectionType where Index: Comparable {
subscript(safe index: Index) -> Generator.Element? {
guard index >= startIndex && index < endIndex else {
return nil
}
return self[index]
}
}
@ilyapuchka
ilyapuchka / XCTest+Expectations
Created December 30, 2015 12:42
Better XCTest expectations
extension XCTestCase {
func fail(expectation: XCTestExpectation?, file: String = __FILE__, line: UInt = __LINE__) {
expectation?.fulfill()
self.recordFailureWithDescription("Expectation failed: \(expectation)", inFile: file, atLine: line, expected: false)
}
func fulfill(expectation: XCTestExpectation?, @autoclosure condition: ()->Bool = true, file: String = __FILE__, line: UInt = __LINE__) {
guard let expectation = expectation else { return }
@ilyapuchka
ilyapuchka / EmbededTableView
Created December 28, 2015 16:14
UITableView subclass that wraps it's content
class EmbededTableView: UITableView {
override func intrinsicContentSize() -> CGSize {
layoutIfNeeded()
return CGSizeMake(UIViewNoIntrinsicMetric, self.contentSize.height)
}
override func reloadData() {
super.reloadData()
invalidateIntrinsicContentSize()
class A {
let factory: ()->()
var closure: ((b: B)->())?
init(factory: ()->()) {
self.factory = factory
}
deinit {
class Stub<ArgumentsType> {
let argumentsMatcher: ArgumentsType -> Bool
init(matcher: ArgumentsType -> Bool) {
self.argumentsMatcher = matcher
}
}
var stubs = [Any]()
@ilyapuchka
ilyapuchka / gist:90bd3954c673cc55ad4e
Created November 5, 2015 11:33
CFDictionary and Swift 2.1
//In Swift 1.x this works and `properties` are casted to `[NSObject: AnyObject]`:
func imageOwnerWithURL(url: NSURL) -> ZMImageOwner! {
if let source = CGImageSourceCreateWithURL(url, nil),
properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [NSObject: AnyObject],
imageWidth = (properties[kCGImagePropertyPixelWidth] as? NSNumber)?.floatValue,
imageHeight = (properties[kCGImagePropertyPixelHeight] as? NSNumber)?.floatValue,
data = NSData(contentsOfURL: url) where acceptableSourceType(source) {
return ImageOwner(data: data, size: CGSizeMake(CGFloat(imageWidth), CGFloat(imageHeight)), nonce: self.nonce)
}