Skip to content

Instantly share code, notes, and snippets.

View Pretz's full-sized avatar

Alex Pretzlav Pretz

View GitHub Profile
import UIKit
func allOptions<T: RawOptionSetType where T.RawValue == UInt>(optionSet: T) -> [T] {
var options = [T]()
var mask: UInt = 1
while mask != 0 {
if let newOption = T(rawValue: mask) where (optionSet & newOption) == newOption {
options.append(newOption)
}
mask <<= 1
// Playground - noun: a place where people can play
import UIKit
class LumaJSONObject: Printable {
var value: AnyObject?
subscript(index: Int) -> LumaJSONObject? {
return (value as? [AnyObject]).map { LumaJSONObject($0[index]) }
}
@Pretz
Pretz / override.swift
Created February 23, 2015 22:38
Swift 1.2 override
import Foundation
class Something: NSObject {
func fun(string: String) {
println(string)
}
// error: method 'fun' redeclares Objective-C method 'fun:'
func fun(int: Int) {
@Pretz
Pretz / ModelFactory.swift
Created January 28, 2015 21:39
strongly typed mock model constructor for Swift tests
struct MockFile<T> {
let fileName: String
init(_ fileName: String) {
self.fileName = fileName
}
}
struct Mocks {
static let UserWithProfile = MockFile<User>("registrations_mock")
import Foundation
class MyDictionary : NSDictionary {
convenience init(beAwesome:Bool) {
if (beAwesome) {
self.init(object: true, forKey:"awesome")
} else {
self.init(object: false, forKey:"awesome")
}
}
@Pretz
Pretz / .clang-format.yaml
Last active August 29, 2015 14:13
.clang-format
BasedOnStyle: Chromium
IndentWidth: 4
UseTab: Never
ColumnLimit: 150
ObjCSpaceAfterProperty: true
PointerBindsToType: false
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: true
SpacesInContainerLiterals: false
AllowShortFunctionsOnASingleLine: false
@Pretz
Pretz / keybase.md
Created October 28, 2014 14:36
Verifying myself

Keybase proof

I hereby claim:

  • I am pretz on github.
  • I am pretz (https://keybase.io/pretz) on keybase.
  • I have a public key whose fingerprint is 9B4A E36E 43C7 87E1 EAD1 2B6A 6D83 84D4 CFE7 7658

To claim this, I am signing this object:

@Pretz
Pretz / let-case.swift
Created October 24, 2014 21:59
Best way to initialize a `let` variable using a case statement?
let inView: UIView = {
switch display {
case .InWindow:
return self.view.window!
case .AboveKeyboard:
return UIApplication.sharedApplication().windows[1] as UIView
default:
return self.view
}
}()
@Pretz
Pretz / keyboardInfoFromNotification.swift
Last active December 9, 2015 21:41
Gets the info from a UIKeyboardWill(Show/Hide)Notification and returns it in a not-terrible tuple
public func keyboardInfoFromUserInfo(userInfo: [NSObject: AnyObject]) -> (beginFrame: CGRect, endFrame: CGRect, animationCurve: UIViewAnimationOptions, animationDuration: Double)? {
if let beginFrameValue = userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue,
let endFrameValue = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
let animationCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber,
let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber {
return (
beginFrame: beginFrameValue.CGRectValue(),
endFrame: endFrameValue.CGRectValue(),
animationCurve: UIViewAnimationOptions(rawValue: animationCurve.unsignedIntegerValue << 16),
animationDuration: animationDuration.doubleValue)
// Playground - noun: a place where people can play
import UIKit
struct ApiResponse {
let success: Bool
let values: [[NSObject: AnyObject]]
}
class BaseModel: NSObject {