This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func runningTime(clousure: () -> Void) { | |
let start = DispatchTime.now() | |
clousure() | |
let end = DispatchTime.now() | |
let runTime = CLongDouble(end.uptimeNanoseconds - start.uptimeNanoseconds) / 1e6 | |
print("runTime: \(runTime) ms") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIApplication { | |
static func setRootController(_ viewController: UIViewController) { | |
guard | |
let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene, | |
let window = scene.windows.filter({ $0.isKeyWindow }).first | |
else { return } | |
window.rootViewController = viewController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIViewController { | |
static func fromStoryboard(_ controllerIdentifier: String? = nil) -> Self? { | |
let name = String(describing: classForCoder()) | |
let suffix = "ViewController" | |
let storyboardName = String(name.dropLast(suffix.count)) | |
let storyboard = UIStoryboard(name: storyboardName, bundle: nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIColor { | |
public convenience init?(hex: String) { | |
let r, g, b: CGFloat | |
if hex.hasPrefix("#") { | |
let start = hex.index(hex.startIndex, offsetBy: 1) | |
let hexColor = String(hex[start...]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ProgressView { | |
private lazy var progressView: UIProgressView = { | |
let progressView = UIProgressView(frame: CGRect(x: 15, y: 20, width: 240, height: 8)) | |
return progressView | |
}() | |
private lazy var alert: UIAlertController = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CardPagingLayout.swift | |
// | |
// Created by Ahmed Fathi on 5/29/20. | |
// Copyright © 2020 Ahmed Fathi. All rights reserved. | |
// | |
import UIKit | |
class CardPagingLayout: UICollectionViewLayout { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source https://stackoverflow.com/a/54237526 | |
// MARK: Source Answer | |
extension UITableView { | |
//Variable-height UITableView tableHeaderView with autolayout | |
func layoutTableHeaderView() { | |
guard let headerView = self.tableHeaderView else { return } | |
headerView.translatesAutoresizingMaskIntoConstraints = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Optional where Wrapped == String { | |
/// Return true if the string value is not nil and not empty string | |
var exists: Bool { | |
switch self { | |
case .none: | |
return false | |
case .some(let string): | |
return !string.isEmpty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
private var timer: Timer? | |
func throttle(_ interval: TimeInterval, block: @escaping (() -> Void)) { | |
timer?.invalidate() | |
timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false, block: { _ in | |
block() | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// All the primes number below & including 10e7 using Sieve algorithm O(n log log n). | |
/// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes | |
func sieve() -> [Int] { | |
let limit = 10000000 // 10e7 | |
// Create an array assumping all numbers are primes | |
var isPrime = Array(repeating: true, count: limit) | |
// Mark trivial cases as not primes | |
isPrime[0] = false |
NewerOlder