Skip to content

Instantly share code, notes, and snippets.

View Pretz's full-sized avatar

Alex Pretzlav Pretz

View GitHub Profile
@Pretz
Pretz / Example.swift
Created February 16, 2022 05:48
StringCodable
struct User: Codable, Equatable {
@StringDecodable
var identifier: Int
@StringCodable
var anotherID: UInt64
}
let user = User(identifier: 100, anotherID: 1030402030)
user.anotherID
@Pretz
Pretz / dynamictype.swift
Created December 21, 2015 02:58
Example of using `===` to compare Types in 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 December 8, 2015 23:37
UIViewController Playground: because getting a UIViewController/UINavigationController to display properly in a playground is a little non-obvious
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 May 9, 2022 10:58
A comparison of different JSON Object Mapping Libraries for Swift

Swift JSON Object Mappers

struct User {
  let id: Int
  let name: String
  let email: String?
  let role: Role
@Pretz
Pretz / dynamicextension.swift
Created September 29, 2015 20:56
Swift Bug w/ Protocol Extensions
protocol SomeValue {
var name: String? { get }
func getName() -> String?
}
extension SomeValue {
var name: String? {
return nil
}
func getName() -> String? {
@Pretz
Pretz / get.swift
Created September 24, 2015 18:21
Fetch items from a Swift collection using negative indices
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
}
@Pretz
Pretz / strutter.swift
Last active August 29, 2015 14:25
Strutter
//: 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 June 2, 2015 21:05
all of the windows-only games I have on steam
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
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
}
import Foundation
public func SelectTypedItemFromArray<T>(array: [AnyObject]) -> T? {
for eachItem in array {
if let typedItem = eachItem as? T {
return typedItem
}
}
return nil
}