Skip to content

Instantly share code, notes, and snippets.

View avdyushin's full-sized avatar

Grigory Avdyushin avdyushin

View GitHub Profile
@avdyushin
avdyushin / deviceDetection.m
Created September 11, 2013 06:56
Detect which device is running app
#define is_iPad ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
#define is_iPhone ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone )
#define is_iPhone5 ( is_iPhone && [[UIScreen mainScreen] bounds].size.height == 568.0f )
#define is_Retina ( [[UIScreen mainScreen] scale] == 2.0f )
@avdyushin
avdyushin / debugLog.m
Created September 11, 2013 13:00
Nice debug log wrapper for NSLog()
#define __FILE_NAME__ [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String]
#ifdef DEBUG
#define DLog( s, ... ) NSLog( @"%s:%d %s %@", \
__FILE_NAME__, \
__LINE__, \
__PRETTY_FUNCTION__, \
[NSString stringWithFormat:(s), ##__VA_ARGS__] )
@avdyushin
avdyushin / HttpClient.swift
Created January 25, 2016 12:17
Demo HTTP client for RESTful services in Swift 2.0
import Foundation
class HttpClient: NSObject {
let logging = true
let session = NSURLSession.sharedSession()
func makeRequest(request: NSURLRequest, completionHandler: (NSDictionary?, NSURLResponse?, NSError?) -> Void) {
logRequest(request)
@avdyushin
avdyushin / delayedCall.m
Created September 2, 2013 07:21
Delayed function call using Grand Central Dispatch
double d = 2.0f; // Delay
dispatch_time_t t = dispatch_time(DISPATCH_TIME_NOW, d * NSEC_PER_SEC);
dispatch_after(t, dispatch_get_main_queue(), ^(void){
// Call any function
});
@avdyushin
avdyushin / randomHexColor.js
Created September 2, 2013 07:18
Generates random hex color string for CSS style
function randomHexColor() {
var v = ( Math.floor( Math.random() * 0xFFFFFF ) ).toString(16);
while ( v.length < 6 ) { v = '0' + v; }
return ( '#' + v );
}
@avdyushin
avdyushin / Functional.swift
Last active March 22, 2016 09:08
Functional Swift Cheat Sheet
// Product of list
func product(list: [Double]) -> Double {
return list.reduce(1, combine: *)
}
// Sum of list
func sum(list: [Int]) -> Int {
return list.reduce(0, combine: +)
}
@avdyushin
avdyushin / string.swift
Last active May 27, 2016 09:23
Swift extension for string manipulation
//
// String 1.0
// Created by Caleb Hess on 2/22/16.
//
public extension String {
public var count: Int {
return self.characters.count
}
@avdyushin
avdyushin / UIImageExtensions.swift
Last active May 31, 2016 11:33
UIImage missed resize method via Swift extensions
extension UIImage {
convenience init?(color: UIColor, size: CGSize = CGSizeMake(1, 1)) {
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
self.init(CGImage: UIGraphicsGetImageFromCurrentImageContext().CGImage!)
UIGraphicsEndImageContext()
@avdyushin
avdyushin / swift-kvo-example.swift
Created June 21, 2016 14:37 — forked from correia/swift-kvo-example.swift
A quick example for how to use Foundation style KVO from Swift. (Official documentation from Apple is forthcoming.)
//
// Swift-KVO
//
// Created by Jim Correia on 6/5/14.
// Copyright (c) 2014-2015 Jim Correia. All rights reserved.
//
// Update: 6/17/2014
//
// KVOContext has gone away; use the same idiom you'd use from Objective-C for the context
//
@avdyushin
avdyushin / convert.sh
Created July 11, 2016 13:51
Covert paymentfont.css to Swift cases
#!/bin/sh
if [ -z $1 ]; then
echo "Usage: ${0} file"
exit -1
fi
cat $1 | \
grep .pf- | \
sed -e 's/.pf-//g' | \