Skip to content

Instantly share code, notes, and snippets.

// Objective-C:
user.userId = @"unknown";
if ([object isKinfOfClass:[NSDictionary class]]) {
if ([[((NSDictionary *)object) objectForKey:@"user_id"] isKindOfClass:[NSString class]]) {
user.userId = (NSString *)[((NSDictionary *)object) objectForKey:@"user_id"];
}
}
class SSNTextField: UITextField {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
registerForNotifications()
}
@VojtaStavik
VojtaStavik / gist:5262141f98100637024d
Last active March 17, 2017 18:27
Keeps the main loop running for swift scripts
class MainProcess {
var shouldExit = false
func start () {
// do your stuff here
// set shouldExit to true when you're done
}
}
var runLoop : NSRunLoop
@VojtaStavik
VojtaStavik / CustomArray.swift
Last active March 2, 2016 10:27
Simple naive implementation of custom Array-like structure
import Foundation
struct MyArray<Element> : CustomStringConvertible {
var description: String {
var mutableDescription = "["
for i in 0..<count {
mutableDescription += "\"\(elementAtIndex(i)) "
if i != count - 1 {
mutableDescription += ", "
import Foundation
class PointerBox<Element> {
var pointer: UnsafeMutablePointer<Element>
let count: Int
init(pointer: UnsafeMutablePointer<Element>, count: Int) {
self.pointer = pointer
self.count = count
}
deinit {
2 ==> 0 1 0
6 ==> 1 1 0
2 ==> 0 1 0
6 ==> 1 1 0
----------------
Different? ✓ ✗ ✗
func calculateHammingDistance(x: Int, y: Int) -> Int {
// Step 1: Find different bits
// Step 2: Count them
}
func calculateHammingDistance(x: Int, y: Int) -> Int {
// Step 1: Find different bits
let differentBits = x ^ y
// Step 2: Count them
}
2 ==> 0 1 0
6 ==> 1 1 0
----------------
2 ^ 6 ==> 1 0 0 // 100 in binary represents number 4