Skip to content

Instantly share code, notes, and snippets.

View ilyapuchka's full-sized avatar

Ilya Puchka ilyapuchka

View GitHub Profile
protocol HasTitle {
var ht_title: String {get set}
}
extension UIBarButtonItem: HasTitle {
var ht_title: String {
get {
return self.title ?? ""
}
set {
@ilyapuchka
ilyapuchka / gist:1ae19259161a91f3a8a8
Created March 16, 2015 21:32
Swift Playgrounds & iOS Simulator
import UIKit
struct MainScene {
let vc: UIViewController
let nc: UINavigationController
init(vc: UIViewController) {
self.vc = vc
self.nc = UINavigationController(rootViewController: vc)
}
}
@ilyapuchka
ilyapuchka / gist:fcb4c50ca5fb49ecc072
Last active August 29, 2015 14:25
Guess what it does
func z_map<S : SequenceType, T>(source: S, transform: (S.Generator.Element) -> T?) -> [T] {
// easy, right? =)
return reduce(source, []) { r, i in map( transform(i) ) { r + [$0] } ?? r }
}
@ilyapuchka
ilyapuchka / Extensions.swift
Last active October 29, 2015 11:44
Swift extensions for classes and structs
protocol ProtocolA {}
protocol ProtocolB: ProtocolA {}
protocol ProtocolC {
func act()
}
protocol ProtocolD {
typealias V
func act()
}
@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)
}
class Stub<ArgumentsType> {
let argumentsMatcher: ArgumentsType -> Bool
init(matcher: ArgumentsType -> Bool) {
self.argumentsMatcher = matcher
}
}
var stubs = [Any]()
class A {
let factory: ()->()
var closure: ((b: B)->())?
init(factory: ()->()) {
self.factory = factory
}
deinit {
@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()
@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 / 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]
}
}