Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Alex Pretzlav Pretz

🎯
Focusing
View GitHub Profile
@Pretz
Pretz / dynamictype.swift
Created Dec 21, 2015
Example of using `===` to compare Types in Swift
View dynamictype.swift
class Obj {}
class Obj2: Obj {}
let item1 = Obj(), item2 = Obj2()
if item1.dynamicType === item2.dynamicType {
print("Same subclass")
} else {
print("Different subclass")
}
@Pretz
Pretz / uiviewcontroller-playground.swift
Created Dec 8, 2015
UIViewController Playground: because getting a UIViewController/UINavigationController to display properly in a playground is a little non-obvious
View uiviewcontroller-playground.swift
import UIKit
let vc = UIViewController()
vc.view.backgroundColor = .whiteColor()
vc.navigationItem.title = "This is a view controller"
let searchController = UISearchController(searchResultsController: nil)
//searchController.searchBar.barTintColor = UIColor(red:0.16, green:0.45, blue:0.72, alpha:1)
@Pretz
Pretz / swift-json.md
Last active Jun 21, 2017
A comparison of different JSON Object Mapping Libraries for Swift
View swift-json.md

Swift JSON Object Mappers

Argo

struct User {
  let id: Int
  let name: String
  let email: String?
  let role: Role
@Pretz
Pretz / dynamicextension.swift
Created Sep 29, 2015
Swift Bug w/ Protocol Extensions
View dynamicextension.swift
protocol SomeValue {
var name: String? { get }
func getName() -> String?
}
extension SomeValue {
var name: String? {
return nil
}
func getName() -> String? {
@Pretz
Pretz / get.swift
Created Sep 24, 2015
Fetch items from a Swift collection using negative indices
View get.swift
import Swift
extension CollectionType where Index: SignedNumberType, Index == Index.Distance {
func get(var position: Self.Index) -> Self.Generator.Element? {
if position < 0 {
position = self.endIndex.advancedBy(position)
}
return (indices ~= position) ? self[position] : nil
}
View strutter.swift
//: Playground - noun: a place where people can play
import UIKit
infix operator |=| { associativity left }
infix operator |=>| { associativity left }
infix operator |=<| { associativity left }
private func _install(constraint: NSLayoutConstraint) {
// Only disable AutoresizineMask on left item, secondItem may need it enabled
@Pretz
Pretz / gist:6762cfd25e4498c1c5fb
Created Jun 2, 2015
all of the windows-only games I have on steam
View gist:6762cfd25e4498c1c5fb
Age of Empires II HD
Age of Empires® III: Complete Collection
Anno 2070™
Aquaria
Assassin's Creed® Revelations
Banished
BioShock™
Borderlands
Crysis
Crysis 2 - Maximum Edition
View select-any.swift
public func SelectTypedItemFromArray<T, U: SequenceType>(type t: T.Type, array: U) -> T? {
for eachItem in array {
if let typedItem = eachItem as? T {
return typedItem
}
}
return nil
}
View infer-return.swift
import Foundation
public func SelectTypedItemFromArray<T>(array: [AnyObject]) -> T? {
for eachItem in array {
if let typedItem = eachItem as? T {
return typedItem
}
}
return nil
}
View allOptions.swift
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