Skip to content

Instantly share code, notes, and snippets.

@czars
czars / AsynchronousOperation.swift
Created July 16, 2021 02:22 — forked from wongzigii/AsynchronousOperation.swift
Subclass of NSOperation (Operation) to make it asynchronous in Swift 3, 4, 5
// Created by Vasily Ulianov on 09.02.17, updated in 2019.
// License: MIT
import Foundation
/// Subclass of `Operation` that adds support of asynchronous operations.
/// 1. Call `super.main()` when override `main` method.
/// 2. When operation is finished or cancelled set `state = .finished` or `finish()`
open class AsynchronousOperation: Operation {
public override var isAsynchronous: Bool {
@czars
czars / debounce.swift
Created July 16, 2021 02:21 — forked from wongzigii/debounce.swift
Debounce Function for Swift 3 (implemented with OperationQueue)
extension OperationQueue {
/// Creates a debounced function that delays invoking `action` until after `delay` seconds have elapsed since the last time the debounced function was invoked.
///
/// - Parameters:
/// - delay: The number of seconds to delay.
/// - underlyingQueue: An optional background queue to run the function
/// - action: The function to debounce.
/// - Returns: Returns the new debounced function.
open class func debounce(delay: TimeInterval, underlyingQueue: DispatchQueue? = nil, action: @escaping () -> Void) -> (() -> Void) {
//
// FlowControllerView.swift
// NavigationFlow
//
// Created by Nick McConnell on 6/7/20.
// Copyright © 2020 Nick McConnell. All rights reserved.
//
import SwiftUI
@czars
czars / chunk swift array
Created September 24, 2019 08:36
split array into chunks of array
extension Array {
func chunked(into size: Int) -> [[Element]] {
return stride(from: 0, to: count, by: size).map {
Array(self[$0 ..< Swift.min($0 + size, count)])
}
}
}
@czars
czars / git_bible.md
Created April 11, 2019 16:38 — forked from dmglab/git_bible.md
how to git

Note: this is a summary of different git workflows putting together to a small git bible. references are in between the text


How to Branch

try to keep your hacking out of the master and create feature branches. the [feature-branch workflow][4] is a good median between noobs (i have no idea how to branch) and git veterans (let's do some rocket sience with git branches!). everybody get the idea!

Basic usage examples

import Foundation
// number of elements
let n = Int(readLine()!)!
// read array and map the elements to integer
let arr = readLine()!.components(separatedBy: " ").map{ Int($0)! }
//edit if any other requirement
@czars
czars / dispatch_once.swift
Created January 9, 2017 03:07 — forked from kristopherjohnson/dispatch_once.swift
Example of using dispatch_once() in Swift
import Foundation
var token: dispatch_once_t = 0
func test() {
dispatch_once(&token) {
println("This is printed only on the first call to test()")
}
println("This is printed for each call to test()")
}
@czars
czars / rx.swift
Created December 9, 2016 16:09
test
var states: Variable<[String]> = Variable([])
states.asObservable()
.filter({ (array) -> Bool in
var valid: Bool = true
for x in array {
valid = x.characters.count > 3
}
print(valid)
return valid
}).subscribe(onNext: { _ in
@czars
czars / ReactiveCocoa signal_then.swift
Last active November 29, 2016 04:36
ReactiveCocoa then and concat
private class func testSignalOne() -> RACSignal {
return RACSignal.createSignal { (subscriber) -> RACDisposable! in
print("test for true")
subscriber.sendNext(true)
subscriber.sendCompleted()
return RACDisposable {
// task.cancel()
}
}
}
@czars
czars / localizedParameterString.swift
Created November 8, 2016 09:16
swift : localized string with parameter
let myString = String(format: NSLocalizedString("account.by_user", comment: "any comment"), "Peter","Larry")