Skip to content

Instantly share code, notes, and snippets.

View johnhatvani's full-sized avatar

John Hatvani johnhatvani

View GitHub Profile
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
func max(a:[Int], n:Int) -> Int
{
var m = a[0]
for i in 0...(n - 1) {
@johnhatvani
johnhatvani / Dates.playground
Last active March 15, 2023 10:12
Swift Playground #1 adding components to Date using operator overloading.
import Foundation
extension Int{
var day: (Int, Calendar.Component) {
return (self, .day)
}
var month: (Int, Calendar.Component) {
return (self, .month)
}
@johnhatvani
johnhatvani / NSObject+SimpleObjectMapping
Last active August 29, 2015 13:57
Simple Object Mapper. A category on NSObject that will construct a instance of your class and assign all values from the dictionary using KVC (make sure your property names are the same as the keys in the dictionary)
#import <objc/runtime.h>
@implementation NSObject (SimpleObjectMapping)
+ (instancetype) objectFromDictionary:(NSDictionary *)dictionary {
NSObject *instance = [[self.class alloc] init];
objc_property_t *properties = class_copyPropertyList(self.class, NULL);
objc_property_t property;
@johnhatvani
johnhatvani / UiButton+Block.
Last active October 10, 2021 04:34
Objective-c runtime fun -- A UIButton that takes a block as an action. The cool thing it does is it adds this action as an instance method at runtime.
#import <objc/runtime.h>
typedef void (^buttonAction)(UIButton *sender);
@implementation UIButton (BlockAction)
+ (UIButton *) buttonWithType:(UIButtonType)type andAction:(buttonAction)action forControlEvents:(UIControlEvents)controlEvents; {
UIButton * button = [UIButton buttonWithType:type];
// suppress undeclared selector warning or else '@selector(action:)' will complain.