Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
wh1pch81n / InitializerProtocol.swift
Last active September 3, 2016 16:43
Initialize an NSObject and set up its properties with a block. Useful for class variables.
import UIKit
protocol Initializer {}
extension Initializer {
func with(bootStrap: (inout Self) -> ()) -> Self {
var s = self
bootStrap(&s)
return s
}
}
@wh1pch81n
wh1pch81n / UIButtonCalbackExtension.swift
Last active September 3, 2016 04:18
UIButton uses an old objective-c style method to add new callbacks which requires selectors. If you would rather use a block based approach then consider the following approach. You can specify a swift closure to be executed when the button receives a touch event.
///////////////
// code example
//-------------
button.callback { _ in
print("hello world") // print "hello world" when touchUpInside event is recieved
}
button.callback(for: [.TouchDown]) { _ in
print("hello world") // print "hello world" when touchDownInside event is recieved
}
@wh1pch81n
wh1pch81n / UISearchBar_Magnifying_GlassColorChange_withSceneDock.swift
Created August 13, 2016 17:21
An alternative implementation for setting the magnifying glass color. You need to add this class to the scene dock of your View Controller in interface builder and connect the IBOutlet to the UISearchBar instance. The color for the component can be set on the object in storyboard. The although this is more work than the other approach you have t…
class UISearchBarMagnifyingGlass: NSObject {
@IBOutlet weak var searchBar: UISearchBar! {
didSet {
if let _color = color, let _searchBar = searchBar {
for i in _searchBar.subviews.first!.subviews {
if let textField = i as? UITextField,
let imageView = textField.leftView as? UIImageView,
let image = imageView.image
{
let coloredImage = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
@wh1pch81n
wh1pch81n / UISearchBar_Magnifying_GlassColorChange.swift
Last active August 15, 2016 02:27
Ever want to change the color of the magnifying glass in the UISearchBar? Well now you can and you can change its color in interface builder!
extension UISearchBar {
/** An easy way to set the magnifying glass color in interface builder
It is recommended to set this in interface Builder,
However if you want to do it programatically you can do it like this:
```
let searchBar = UISearchBar()
searchBar.magnifyingGlassColor = UIColor.red
```
*/
@IBInspectable var magnifyingGlassColor: UIColor {
func supportsKeyboard(lang: String?) -> Bool {
guard let _lang = lang else {
return false
}
let predicates = [
Predicate(format: "SELF == %@", "en"),
Predicate(format: "SELF like[cd] %@", "en-*")
]
for predicate in predicates {
if NSArray(arrayLiteral: _lang).filtered(using: predicate).count > 0 {
@wh1pch81n
wh1pch81n / GenericTableView.swift
Last active December 16, 2017 07:51
Sometimes it is easier to make a tableview generic. And just load it with info, rather than creating a whole class that conforms with UITableViewDataSource and UITableViewDelegate
import UIKit
class MyTableViewCell: UITableViewCell {
@IBOutlet var switchview: UISwitch!
@IBOutlet var sliderview: UISlider!
}
class Section {
var count: Int { return cells.count }
@wh1pch81n
wh1pch81n / Struct_Singleton_3ProtocolSplit.swift
Created July 9, 2016 18:05
And example of using a struct singleton with only static properties. The static properties are enforced with the protocol. We can still use dependency injection which means we can do away with the "sharedInstance" construct.
protocol Singleton3_Protocol {
static var queue: OperationQueue { get }
static func method1()
}
protocol Name {
static var name: String { get set }
}
protocol Singleton3_Protocol2: Name {
@wh1pch81n
wh1pch81n / SingletonSubclassing.swift
Last active February 19, 2022 02:01
The Swift way of doing Singleton inheritance.
import UIKit
class Singleton {
class func sharedInstance() -> Singleton {
struct __ { static let _sharedInstance = Singleton() }
return __._sharedInstance
}
var name: String = String()
}
@wh1pch81n
wh1pch81n / implicitBundle.swift
Created June 10, 2016 03:25
In objective -c you can use [self class] to get the Class of an object. From there you can get the NSBundle. A smart man can use a macro when locating strings and make one such that [self class] is used to get the correct bundle. However, swift does not have macros. Below is a work around that does something similar.
import Foundation
/**
original macro
#define DHLocalizedString(KEY, TABLE, COMMENT) NSLocalizedStringWithDefaultValue(KEY, TABLE, [NSBundle mainBundle], NSLocalizedStringWithDefaultValue(KEY, TABLE, [NSBundle bundleForClass:[self class]]), COMMENT)
*/
func getNameByRemovingPrefixPathAndExtension(name: String) -> String {
let url = NSURL(string: name)!
@wh1pch81n
wh1pch81n / objcGenerics
Created May 22, 2016 03:17
Objective c generics for something that isn't a collection type. Here is an example of producing a view controller from storyboard where you are returned the correct type thanks to generics.
// Making a class since we can not add generics to UIStoryboard nor can we make a category for it.
@interface UIStoryboardHelper <__covariant T:UIViewController *> : UIStoryboard
- (T)vcFromIdentifier:(NSString *)identifier fromStoryboard:(UIStoryboard *)storyboard;
@end
@implementation UIStoryboardHelper
- (UIViewController *)vcFromIdentifier:(NSString *)identifier fromStoryboard:(UIStoryboard *)storyboard {
return [storyboard instantiateViewControllerWithIdentifier:identifier];
}
@end