Skip to content

Instantly share code, notes, and snippets.

@Busta117
Busta117 / ObjectMapperRealmHelper.swift
Last active March 1, 2020 16:56
map Realm arrays into ObjectMapper
//
// ObjectMapperRealmHelper.swift
//
// Created by Santiago Bustamante on 07/27/18.
//
//
import ObjectMapper
import RealmSwift
@Busta117
Busta117 / ObjectMapperRealmHelper.swift
Created February 15, 2017 19:03
transform array from objectMapper parsing to Realm List
//
// ObjectMapperRealmHelper.swift
// Pods
//
// Created by Santiago Bustamante on 10/13/16.
//
//
import ObjectMapper
import RealmSwift
@Busta117
Busta117 / rxbi
Created November 21, 2016 21:25
Bidirectional bind RxSwift, swift 3
infix operator <~> : AssignmentPrecedence
//Bidirectional bind RxSwift, swift 3
public func <~> <E,C: ControlPropertyType>(property: C, variable: Variable<E>) -> Disposable where C.E == E? {
let bindToUIDisposable = variable.asObservable()
.bindTo(property)
let bindToVariable = property
.subscribe(onNext: { n in
if let n = n{
variable.value = n
@Busta117
Busta117 / updateDictionaty
Last active October 25, 2016 20:35
merge 2 dictionaries in 1 with the operator += changes will keep in the left one
/**
add the content of a dictionary into another dictionary
swift 3
*/
func += <K>( dict1:inout [K:Any], dict2:[K:Any]){
dict2.forEach { (key: K, value: Any) in
dict1[key] = value
}
}
@Busta117
Busta117 / DateFormatterISO8601Regex
Created October 24, 2016 05:49
return the right dateString and dateFormat of a ISO8601 to use it in DateFormatter, validated by regular expression, so, you don't need to fight again with those formats
func formatFrom(_ value:String) -> (dateString:String?, dateFormat:String?) {
var regexString = "\\A(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2})" // Mandatory - YYYY-MM-DDTHH:mm
regexString = regexString + ":?(\\d{2})?" // Optional - :ss
regexString = regexString + "[.]?(\\d{1,6})?" // Optional - .nnnnnn
regexString = regexString + "([+-])?(\\d{2})?:?(\\d{2})?|Z" // Optional -[+-]hh:mm or Z
regexString = regexString + "\\z"
let regex: NSRegularExpression?
var matchesResult: [NSTextCheckingResult]?
@Busta117
Busta117 / Image
Last active October 3, 2016 19:16
extension UIImage tint color swift 3
public extension UIView {
/**
Convert current view to image
:returns: return the image
*/
public func convertToImage() -> UIImage {
UIGraphicsBeginImageContext(self.bounds.size);
self.layer.render(in: UIGraphicsGetCurrentContext()!)
let viewImage = UIGraphicsGetImageFromCurrentImageContext()
@Busta117
Busta117 / BaristaISO8601DateTransform
Created October 3, 2016 18:56
objectmapper ISO8601 date transform
import ObjectMapper
import RealmSwift
open class BaristaISO8601DateTransform: TransformType {
public typealias Object = Date
public typealias JSON = String
let dateFormatter: DateFormatter = DateFormatter()
@Busta117
Busta117 / ArrayTransform
Created October 2, 2016 22:06
transform array from objectMapper parsing to Realm List
//transform array from objectMapper parsing to Realm List
public class ArrayTransform<T:RealmSwift.Object where T:Mappable> : TransformType {
public typealias Object = List<T>
public typealias JSON = Array<Any>
public init(){
}
public func transformFromJSON(_ value: Any?) -> List<T>? {
let result = List<T>()
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '3.0'
end
end
end
@Busta117
Busta117 / gist:f7e7e81d9c2098505359
Created June 13, 2015 21:22
address from cords
class func getAddressFromCoords(coords:CLLocationCoordinate2D, complete:(location:Location?, error:NSError?)->()){
var url = "http://maps.googleapis.com/maps/api/geocode/json"
var str = String(format: "%f",coords.latitude) + "," + String(format:"%f",coords.longitude)
var params = ["latlng":str, "sensor":true]
AFHTTPRequestOperationManager().GET(url, parameters: params, success: { (operation:AFHTTPRequestOperation!, response:AnyObject!) -> Void in
let json:JSON = JSON(response)
var loc = Location(dictionary: json)