Skip to content

Instantly share code, notes, and snippets.

View leojkwan's full-sized avatar
🏠
Working from home

Leo Kwan leojkwan

🏠
Working from home
View GitHub Profile
class EmbeddableCustomView: UIView, HasNib {
override init(frame: CGRect) {
super.init(frame: frame)
// Render xib view from code.
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// MARK: Support for instantiation from NIB
public extension HasNib where Self: UIView {
static var nib: UINib {
return UINib(nibName: String(describing: self), bundle: Bundle(for: self))
}
// Adds content loaded from the nib to the end of the
// receiver's list of subviews and adds constraints automatically.
func loadNibContent() {
@leojkwan
leojkwan / HasNib.swift
Last active July 16, 2018 21:07
Initialize custom nib views in a type-safe-er way when .xib and implementation files are named the same.
public protocol HasNib: class {
static var nib: UINib { get }
}
public extension HasNib {
static var nib: UINib {
return UINib(nibName: String(describing: self), bundle: Bundle(for: self))
}
}
@leojkwan
leojkwan / NativeObservable.swift
Created May 5, 2017 05:33
Native Observable Pattern in Swift
import UIKit
class PushedViewController: UIViewController {
var currentWeather: Observable<Double>!
private let disposeBag = MyDisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
import UIKit
import Moya
class ViewController: UIViewController {
private let instaFoodProvider = MoyaProvider<InstaFoodService>()
override func viewDidLoad() {
super.viewDidLoad()
@leojkwan
leojkwan / RequestManager.swift
Last active February 20, 2017 17:07
Shared URLSession Networking Client
import UIKit
public typealias RequestResponse = (_ statusCode:Int, _ data:AnyObject?) -> Void
struct RequestManager {
static func makeHTTPSRequest(
_ token: String?,
postBody: AnyObject? = nil,
methodString:String,
struct Person {
var name:String
}
let leo = Person(name: "Leo")
let nicole = Person(name: "Nicole")
let leonardoButCalledLeo = Person(name: "Leo")
let ashton = Person(name: "Ashton")
import Cocoa
// MARK: Goal- Find the number of times any item appears in array
var tayLyrics = "Nice to meet you, where you been? I could show you incredible things Magic, madness, heaven, sin Saw you there and I thought Oh my God, look at that face You look like my next mistake Love’s a game, want to play? New money, suit & tie"
var stringArray = tayLyrics.componentsSeparatedByString(" ")
var intArray = [1,4,5,2,6,6,6]