Skip to content

Instantly share code, notes, and snippets.

View pavankataria's full-sized avatar

Pavan Kataria pavankataria

View GitHub Profile
@pavankataria
pavankataria / PKNavigationController
Last active December 29, 2015 15:41
Adds a navigation item on all navigation controllers that inherit this class.
//
// PKNavigationController.swift
//
// Created by Pavan Kataria on 29/12/2015.
// Copyright © 2015 Pavan Kataria. All rights reserved.
//
import UIKit
class PKNavigationController: UINavigationController {
@pavankataria
pavankataria / Duplication
Created January 7, 2016 01:58
there's some serious duplication here with this default parameterised methods with the completion handler
class ApiManager {
class func GET(
URLString: URLStringConvertible,
parameters: [String : AnyObject]? = nil,
encoding: ParameterEncoding? = .URL,
headers: [String : String]? = nil,
completionHandler: CompletionHandlerType
){
alamofireManager().request(.GET, URLString, parameters: parameters, encoding: encoding!, headers: headers)
.validate()
    class MyAppCantFunctionUnlessBothPropertiesAreBloodyPresent {
        var cloudId: String?
        var apnsToken: String?
        func pleaseJustSendThisShitToMyServerSoICanBeDoneWith() {
          guard let cloudId = self.cloudId, apnsToken = self.apnsToken else {
              print(“Sighs, nope not ready yet, we’re all probably waiting for that blaady apns token again")
              return
          }
          print(“what took you two so long? Let’s make that request to igors server now")
@pavankataria
pavankataria / HasDisposeBag.swift
Created September 29, 2017 13:51
A protocol for automatically adding a dispose bag to any object that requires it. Uses associated objects.
//
// HasDisposeBag.swift
//
// Created by Pavan Kataria 26/09/2017
// Copyright © 2017 Pavan Kataria. All rights reserved.
protocol HasDisposeBag {
var disposeBag: DisposeBag { get }
}
private struct AssociatedKeys {
@pavankataria
pavankataria / GenericTableViewCell.plagrounds
Created October 6, 2017 13:54
Injecting View into Generic TableViewCell
import Foundation
import UIKit
public protocol CellRepresentable {
static func registerCell(tableView: UITableView)
func dequeueCell(tableView: UITableView, indexPath: IndexPath) -> UITableViewCell
func cellSelected(_ indexPath: IndexPath)
}
//MARK: - Default Implementation
@pavankataria
pavankataria / SortButton.swift
Created November 29, 2017 12:51
A UIButton subclass that renders a sort button.
fileprivate extension UIControlState {
static let unspecified = UIControlState(rawValue: 1 << 16)
static let ascending = UIControlState(rawValue: 1 << 17)
static let descending = UIControlState(rawValue: 1 << 18)
}
class SortButton: UIButton {
private var customState: UIControlState = .unspecified {
didSet {
setNeedsLayout()
func processInput(_ stringNumber: String?) -> Int? {
guard let unwrappedStringNumber = stringNumber {
return nil
}
return Int(unwrappedStringNumber)
}
func processInput(_ stringNumber: String?) -> Int? {
return stringNumber.flatMap { Int($0) }
}
// In the flatmap method it would look something like this
switch self {
case .some(let unwrapped):
return transform(unwrappedValue)
case .none:
return nil
}
let balanceToConvert: String?
...
if let inputEntry = balance, inputEntry.sign == .negative {
balanceToConvert = Int(inputEntry.input) // Can return nil
.flatMap(Int.init) // Convert to Int if not nil
.flatMap { $0 * -1 } // flip sign if not nil
.flatMap(String.init) // convert back to String if not nil
}
...