Skip to content

Instantly share code, notes, and snippets.

View demonar's full-sized avatar
😄
(¯`•._.•«<ÐêMøN>»•._.•´¯) -> Building mobile apps with Kotlin and Swift

Alejandro Moya demonar

😄
(¯`•._.•«<ÐêMøN>»•._.•´¯) -> Building mobile apps with Kotlin and Swift
View GitHub Profile
@demonar
demonar / DictionaryUtils.swift
Last active August 31, 2015 23:58
Swift Static method used to remove nulls from dictionaries
import Foundation
class DictionaryUtils {
class func removeNullsFromDictionary(origin:[String:AnyObject]) -> [String:AnyObject] {
var destination:[String:AnyObject] = [:]
for key in origin.keys {
if origin[key] != nil && !(origin[key] is NSNull){
if origin[key] is [String:AnyObject] {
destination[key] = self.removeNullsFromDictionary(origin[key] as! [String:AnyObject])
} else if origin[key] is [AnyObject] {
import Foundation
extension NSDate {
func coolDate() -> String {
if self.minutesFromNow() < 1 {
return "Now"
} else if self.minutesFromNow() < 60 {
return numberWithSuffix(self.minutesFromNow(), "m", false)
} else if self.hoursFromNow() < 24 {
return numberWithSuffix(self.hoursFromNow(), "h", false)
import Foundation
extension Float {
func format(f: String) -> String {
return String(format: "%\(f)f", self)
}
mutating func roundTo(decimals: Int) {
let divisor = pow(10.0, Float(decimals))
self = round(self * divisor) / divisor
}
import UIKit
extension UIView {
func scrollToY(y: CGFloat) {
scrollToY(y, velocity: 0.25)
}
func scrollToView(view: UIView) {
scrollToView(view, velocity: 0.25)
import UIKit
private var kAssociationKeyNextField: UInt8 = 0
extension UITextField {
@IBOutlet var nextField: UITextField? {
get {
return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField
}
set(newField) {
@demonar
demonar / ReactiveQuickHelp.swift
Last active December 12, 2017 22:54
Reactive cocoa in swift 3/4
//Always import
import ReactiveCocoa //Cocoa extensions
import ReactiveSwift //primitives
//Acces UI extensions with .reactive
self.textField.reactive.
//A text listener in a textfield
// self.reactive.lifetime will make the listener to expire on dealloc
@demonar
demonar / SyncRetrofitCallWithResult.kt
Created October 8, 2019 23:59
This method adds an extension to retrofit to return a Result<T> from execute()
inline fun <reified T> Call<T>.executeForResult(): Result<T> {
return try {
val response = execute()
Result.Success(this, response)
} catch (e: Exception) {
Result.Failure(this, e)
}
}
//let's say that we have a login method
val call = service.login(username, pass)
//we call the new enqueue
call.enqueue { result ->
when(result) {
is Result.Success -> {
//myData will have the type passed from the service method
val myData = result.response.body()
}
is Result.Failure -> {
inline fun <reified T> Call<T>.enqueue(crossinline result: (Result<T>) -> Unit) {
enqueue(object: Callback<T> {
override fun onFailure(call: Call<T>, error: Throwable) {
result(Result.Failure(call, error))
}
override fun onResponse(call: Call<T>, response: Response<T>) {
result(Result.Success(call, response))
}
})
sealed class Result<T> {
data class Success<T>(val call: Call<T>, val response: Response<T>): Result<T>()
data class Failure<T>(val call: Call<T>, val error: Throwable): Result<T>()
}