Skip to content

Instantly share code, notes, and snippets.

View dbonates's full-sized avatar
🏠
Working from home

Daniel Bonates dbonates

🏠
Working from home
  • Self Industry
  • Florianopolis, Brazil
View GitHub Profile
{
"valid_promotion": {
"created_at" : "2016-01-11T19:14:21.123-02:00",
"discount_percentage" : 15,
id : 16719297,
"product_current_price_cents" : 12300,
"product_id" : 14454151,
"product_original_price_cents" : 12300,
"product_promotional_price_cents" : 10400,
"promotion_expire_at" : "2016-01-14T23:59:59.999-02:00",
@dbonates
dbonates / saveimage.swift
Created January 25, 2016 16:36
saving uiimage to path
let pngImageData = UIImagePNGRepresentation(image)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let imagePath = documentsURL.URLByAppendingPathComponent("remela.png")
pngImageData!.writeToFile(imagePath.path!, atomically: true)
// 1. SynchonousRequest
let urlPath: String = "YOUR_URL_HERE"
var url: NSURL = NSURL(string: urlPath)!
var request1: NSURLRequest = NSURLRequest(URL: url)
var response: AutoreleasingUnsafeMutablePointer<NSURLResponse? >= nil
var error: NSErrorPointer = nil
var dataVal: NSData = NSURLConnection.sendSynchronousRequest(request1, returningResponse: response, error:nil)!
var err: NSError
@dbonates
dbonates / test.swift
Created January 26, 2016 03:50 — forked from jepers/test.swift
Example of something that takes a long time to type check.
// This little program takes ~ 6 seconds to compile on my MacBook Pro Late 2013.
// It compiles and works as expected. But the type checker spends a lot of time
// on the function at the end.
// In my actual project, I have much more code like this, and it results in very
// long compile times. So I'd like to know if there is something I can do to
// speed that up.
protocol DefaultInitializable { init() }
extension Float : DefaultInitializable {}
@dbonates
dbonates / git_branch.sh
Created January 28, 2016 15:51 — forked from dciccale/git_branch.sh
Bash script to get the current git branch and last commit
#!/usr/bin/env bash
# checks if branch has something pending
function parse_git_dirty() {
git diff --quiet --ignore-submodules HEAD 2>/dev/null; [ $? -eq 1 ] && echo "*"
}
# gets the current git branch
function parse_git_branch() {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(parse_git_dirty)/"
@dbonates
dbonates / parseJSON.swift
Last active January 29, 2016 15:10
parse json string in Swift
func parseJSON(jsonString: String) -> AnyObject {
// ensure that some data arrived
guard let validData = jsonString.dataUsingEncoding(NSUTF8StringEncoding) where validData.length > 0 else {
print("**** NO DATA to be parse!")
return NSNull()
}
@dbonates
dbonates / user_data_demo.json
Last active February 29, 2016 13:57
json example for an article
[
{
"id": 1,
"user_avatar": "http://placehold.it/128/4597CE/F7E42D/&text=FS",
"user_fullname": "Frank Simpson"
},
{
"id": 2,
"user_avatar": "http://placehold.it/128/4597CE/F3F4F5/&text=HH",
!function(e,n,t){function r(e,n){return typeof e===n}function o(){var e,n,t,o,i,s,a;for(var f in C)if(C.hasOwnProperty(f)){if(e=[],n=C[f],n.name&&(e.push(n.name.toLowerCase()),n.options&&n.options.aliases&&n.options.aliases.length))for(t=0;t<n.options.aliases.length;t++)e.push(n.options.aliases[t].toLowerCase());for(o=r(n.fn,"function")?n.fn():n.fn,i=0;i<e.length;i++)s=e[i],a=s.split("."),1===a.length?Modernizr[a[0]]=o:(!Modernizr[a[0]]||Modernizr[a[0]]instanceof Boolean||(Modernizr[a[0]]=new Boolean(Modernizr[a[0]])),Modernizr[a[0]][a[1]]=o),g.push((o?"":"no-")+a.join("-"))}}function i(e){var n=w.className,t=Modernizr._config.classPrefix||"";if(x&&(n=n.baseVal),Modernizr._config.enableJSClass){var r=new RegExp("(^|\\s)"+t+"no-js(\\s|$)");n=n.replace(r,"$1"+t+"js$2")}Modernizr._config.enableClasses&&(n+=" "+t+e.join(" "+t),x?w.className.baseVal=n:w.className=n)}function s(e){return e.replace(/([a-z])-([a-z])/g,function(e,n,t){return n+t.toUpperCase()}).replace(/^-/,"")}function a(e,n){return!!~(""+e).indexOf(
@dbonates
dbonates / cachedThreadLocalObjectWithKey.swift
Created June 20, 2016 20:22 — forked from kristopherjohnson/cachedThreadLocalObjectWithKey.swift
Swift utility function to create and reuse a thread-local object
import Foundation
/// Return a thread-local object, creating it if it has not already been created
///
/// :param: create closure that will be invoked to create the object
/// :returns: object of type T
public func cachedThreadLocalObjectWithKey<T: AnyObject>(key: String, create: () -> T) -> T {
if let threadDictionary = NSThread.currentThread().threadDictionary {
if let cachedObject = threadDictionary[key] as T? {
return cachedObject