Skip to content

Instantly share code, notes, and snippets.

Andrey Panov AndreyPanov

Block or report user

Report or block AndreyPanov

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@AndreyPanov
AndreyPanov / NSDictionaryExtension.swift
Last active Oct 10, 2017
Convert objc NSDictionary to swift dictionary
View NSDictionaryExtension.swift
extension NSDictionary {
var swiftDictionary: [String : AnyObject] {
var swiftDictionary: [String : AnyObject] = [:]
let keys = self.allKeys.flatMap { $0 as? String }
for key in keys {
let keyValue = self.value(forKey: key) as AnyObject
swiftDictionary[key] = keyValue
}
return swiftDictionary
@AndreyPanov
AndreyPanov / Debounce.swift
Created Aug 10, 2017
Debounce function for Swift 3
View Debounce.swift
func debounce(_ action: @escaping () -> Void) {
let lastFireTime = DispatchTime.now()
let dispatchDelay = TimeInterval(0.3)
DispatchQueue.main.asyncAfter(deadline: .now() + dispatchDelay) {
let now = DispatchTime.now()
let when = lastFireTime + dispatchDelay
if now >= when {
action()
}
View NewRepository.swift
final class Repository: Chainable {
var before: (()->())?
private let dataProdider: DataProvider
init(dataProdider: DataProvider = .init()) {
self.dataProdider = dataProdider
}
func getItem(onSuccess: @escaping (Item)->(), onError: @escaping (Error)->()) {
//background async request
View Chainable.swift
protocol Chainable: class {
var before: (()->())? { get set }
func before(_ callback: @escaping ()->()) -> Self
func invoke()
}
extension Chainable {
@discardableResult
View onViewLoadedNew.swift
func onViewLoaded() {
view?.showLoadingIndicator()
repository
.before({ [weak self] in self?.view?.hideLoadingIndicator() })
.getItem(
onSuccess: { [weak self] item in
print(item.description)
},
onError: { [weak self] error in
print(error.localizedDescription)
View onViewLoaded.swift
func onViewLoaded() {
view?.showLoadingIndicator()
repository.getItem(
onSuccess: { [weak self] item in
self?.view?.hideLoadingIndicator()
print(item.description)
},
onError: { [weak self] error in
self?.view?.hideLoadingIndicator()
print(error.localizedDescription)
View OldRepository.swift
final class Repository {
private let dataProdider: DataProvider
init(dataProdider: DataProvider = .init()) {
self.dataProdider = dataProdider
}
func getItem(onSuccess: @escaping (Item)->(), onError: @escaping (Error)->()) {
//background async request
dataProvider.request.items { [weak self] data, error in
if let item = ItemBuilder(with: data) {
View ApplyStyle.swift
func applyStyle() {
StyleBuilder()
.guide()
.forEach { setStyle(with: $0) }
}
private func setStyle(with guide: Guide) {
switch guide {
case .headerLabel(let closure),
.bodyLabel(let closure):
View Perform.swift
func perform<T>(with identifier: String, closure: (T) -> Void) {
Mirror(reflecting: self).children.forEach { property in
guard
property.label?.contain(identifier) == true,
let value = property.value as? T
else { return }
//run closure
closure(value)
}
}
View funcGuide.swift
func guide() -> [Guide] {
return [
.headerLabel(headerLabel),
.bodyLabel(bodyLabel),
.loginTextField(loginTextField)
]
}
You can’t perform that action at this time.