Skip to content

Instantly share code, notes, and snippets.

@chuthan20
chuthan20 / UIColor+fromHexString.swift
Last active August 27, 2016 16:41
Converting from hex to UIColor
extension UIColor {
static func from(hex: String) -> UIColor? {
assert(hex.characters.first! == "#", "Maybe not a valid colour \(hex)")
var val = hex.substringFromIndex(hex.startIndex.advancedBy(1))
switch val.characters.count {
case 3: val.appendContentsOf("f"); fallthrough
case 4: val = val.characters.map{"\($0)\($0)"}.joinWithSeparator("")
case 6: val.appendContentsOf("ff")
case 8: do {}
default: return nil // not proper format: #rgb, #rgba, #rrggbb, #rrggbbaa
@chuthan20
chuthan20 / NSJSONSerialization-wrapper.swift
Created March 10, 2016 03:42
Wrapping NSJSONSerialization to make the methods simpler/easy to use.
//: Playground - noun: a place where people can play
import Foundation
public class JSON {
public static func toNSData(obj: AnyObject) -> NSData? {
return try? NSJSONSerialization.dataWithJSONObject(obj, options: NSJSONWritingOptions(rawValue: 0))
}
public static func toJSONObj(obj: NSData) -> AnyObject? {
@chuthan20
chuthan20 / Swift-dictionary-key-path-access.swift
Created March 10, 2016 03:36
Similar to key-path way to access values in swift dictionary
import Foundation
extension Dictionary {
func JSONValue(path:String) -> AnyObject? {
let dict = self as! NSDictionary
return dict.JSONValue(path)
}
}
@chuthan20
chuthan20 / swift-string-extensions.swift
Last active March 10, 2016 03:36
Swift string extensions
import Foundation
extension String {
subscript(range: NSRange) -> String {
get {
let comp = self
let begin = comp.startIndex.advancedBy(range.location)
let sre = comp.substringWithRange(begin ..< begin.advancedBy(range.length))
return sre
@chuthan20
chuthan20 / sqlite_statement_debug.m
Last active March 10, 2016 03:37
prints out the sqlite3 statement type and name.
+ (void) debugStatement:(sqlite3_stmt *)statement
{
for(int i=0; i<sqlite3_column_count(statement); i++)
{
sqlite3_column_value(statement, i);
const char *name = sqlite3_column_name(statement, i);
const char *type = sqlite3_column_decltype(statement, i);
NSLog(@"%d %s %s", i, name, type);
}