Skip to content

Instantly share code, notes, and snippets.

View evgeniyd's full-sized avatar
🌚

Yevhen Dubinin evgeniyd

🌚
View GitHub Profile
@evgeniyd
evgeniyd / UBIOperator.swift
Last active August 29, 2015 14:06
Override += operator to append a Dictionary with values and keys from another Dictionary
import Foundation
func += <KeyType, ValueType> (inout left: Dictionary<KeyType, ValueType>, right: Dictionary<KeyType, ValueType>) {
for (k, v) in right {
left.updateValue(v, forKey: k)
}
}
@evgeniyd
evgeniyd / UIImageExtensions.swift
Created October 19, 2014 12:49
Factory method to capture view's screenshot and return a UIImage object
import UIKit
extension UIImage {
public class func imageWithScreenshotFromView(view: UIView) -> UIImage! {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.mainScreen().scale)
view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
@evgeniyd
evgeniyd / UBIFormValidators.swift
Created October 22, 2014 07:17
Fields (email, password, set password, form, etc.) validators. Based on WWDC2014 session #229. I'm still working on this.
import Foundation
protocol Validator {
func validateWithError(error: NSErrorPointer) -> Bool
}
///////////////////////////////////////////////
// MARK: - PasswordValidator -
///////////////////////////////////////////////
class PasswordValidator: Validator {
@evgeniyd
evgeniyd / UIColor+CustomColors.swift
Last active August 29, 2015 14:08
Creating UIColor extension in Swift
import UIKit
extension UIColor {
class func customGrayColor() -> UIColor {
let r: CGFloat = CGFloat(41.0/255.0)
let g: CGFloat = CGFloat(41.0/255.0)
let b: CGFloat = CGFloat(41.0/255.0)
return UIColor(red: r, green: g, blue: b, alpha: 1.0)
}
@evgeniyd
evgeniyd / NSDate+MyExtensions.swift
Created November 23, 2014 00:32
The basic principle of converting String => NSDate. The gist has both, convenient initializer and class method to create an instance of NSDate
import Foundation
private var sDateFormatter: NSDateFormatter = NSDateFormatter()
extension NSDate
{
convenience
init?(fromDateString dateString: String) {
sDateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
sDateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
@evgeniyd
evgeniyd / NSNumber+MyExtensions.swift
Created December 11, 2014 00:16
In Objective-C, converting from NSNumber to NSTimeInterval is straightforward by Type-Cast from double. Swift, however, needs more strict way, I would say.
import Foundation
extension NSNumber
{
var timeIntervalValue: NSTimeInterval {
get {
return (self.doubleValue as NSTimeInterval)
}
// TODO: write a setter
}
@evgeniyd
evgeniyd / UIColor+ColorComponents.swift
Created December 16, 2014 02:10
A shorthand for creating UIColor object from components
import UIKit
extension UIColor {
class func colorWithComponents(red redComponent: Float, green: Float, blue: Float, alpha: Float) -> UIColor {
let r: CGFloat = CGFloat(redComponent/255.0)
let g: CGFloat = CGFloat(green/255.0)
let b: CGFloat = CGFloat(blue/255.0)
let a: CGFloat = CGFloat(alpha)
return UIColor(red: r, green: g, blue: b, alpha: a)
@evgeniyd
evgeniyd / MPSTableViewCell.swift
Last active August 29, 2015 14:12
UITableViewCell subclass where each cell could have its own separator w/ many styles (currently only .None and .Default are supported). Note: UITableView object's separator style should be set to "None".
import UIKit
enum MPSTableViewCellSeparator {
case Default, None
// returns views for each style - very convinient
func view() -> UIView? {
switch (self) {
case .None:
return nil
@evgeniyd
evgeniyd / UBINullableSortDescriptor.swift
Last active July 16, 2018 16:59
NSSortDescriptor subclass to sort objects w/ nullable key. These objects appears (unsorted) at the end of the sorted list when ascending == true. Written in Swift, this class merely extends the idea from this SO answer: http://stackoverflow.com/a/11188999/1492173
import Foundation
class UBINullableSortDescriptor: NSSortDescriptor {
override init(key: String, ascending: Bool) {
super.init(key: key, ascending: ascending)
}
required override init(key: String, ascending: Bool, selector: Selector) {
super.init(key: key, ascending: ascending, selector: selector)
#import <Foundation/Foundation.h>
@interface NSString (URLValidation)
- (BOOL)isValidURL;
@end