Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Alex Pretzlav Pretz

🎯
Focusing
View GitHub Profile
View LumaJSON.swift
// 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 Feb 23, 2015
Swift 1.2 override
View override.swift
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 Jan 28, 2015
strongly typed mock model constructor for Swift tests
View ModelFactory.swift
struct MockFile<T> {
let fileName: String
init(_ fileName: String) {
self.fileName = fileName
}
}
struct Mocks {
static let UserWithProfile = MockFile<User>("registrations_mock")
View obj-c-convenience-init.swift
import Foundation
class MyDictionary : NSDictionary {
convenience init(beAwesome:Bool) {
if (beAwesome) {
self.init(object: true, forKey:"awesome")
} else {
self.init(object: false, forKey:"awesome")
}
}
View .clang-format.yaml
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 Oct 28, 2014
Verifying myself
View keybase.md

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 Oct 24, 2014
Best way to initialize a `let` variable using a case statement?
View let-case.swift
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 Dec 9, 2015
Gets the info from a UIKeyboardWill(Show/Hide)Notification and returns it in a not-terrible tuple
View keyboardInfoFromNotification.swift
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)
View generic_callback.swift
// Playground - noun: a place where people can play
import UIKit
struct ApiResponse {
let success: Bool
let values: [[NSObject: AnyObject]]
}
class BaseModel: NSObject {
@Pretz
Pretz / retina_wrap.swift
Last active Nov 14, 2017
Render a view at 2x scale factor in an Xcode playground
View retina_wrap.swift
func retinaWrap(view: UIView, scaleFactor: CGFloat = 2.0) -> UIView {
let scaleTransform = CGAffineTransformMakeScale(scaleFactor, scaleFactor)
let scaledView = UIView(frame: CGRectMake(0, 0, view.bounds.width * scaleFactor, view.bounds.height * scaleFactor))
view.transform = scaleTransform
view.center = CGPoint(x: scaledView.frame.width / 2, y: scaledView.frame.height / 2)
scaledView.addSubview(view)
return scaledView
}