This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIView { | |
func scrollToY(y: CGFloat) { | |
scrollToY(y, velocity: 0.25) | |
} | |
func scrollToView(view: UIView) { | |
scrollToView(view, velocity: 0.25) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
private var kAssociationKeyNextField: UInt8 = 0 | |
extension UITextField { | |
@IBOutlet var nextField: UITextField? { | |
get { | |
return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField | |
} | |
set(newField) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 -> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | |
} |
OlderNewer