Skip to content

Instantly share code, notes, and snippets.

View manas-sharma-1683's full-sized avatar
🔎

manas sharma manas-sharma-1683

🔎
View GitHub Profile
@manas-sharma-1683
manas-sharma-1683 / password_generator.swift
Created January 31, 2021 08:25
Generate a random, strong password.
/*
*
* Generates a random and strong password in the format xxx-xxx-xxx-xxx.
* Note: Not available on WatchOS.
*
*/
import Security
@manas-sharma-1683
manas-sharma-1683 / callbacks.swift
Last active February 25, 2021 22:54
How to wait for two callbacks in swift.
/*
*
* I recommend using RxSwift, Combine or PromiseKit for this kind of flow.
*
*/
import UIKit
import Foundation
func wait<A,B>(_ blockA: @escaping (@escaping (Result<A,Error>) -> Void) -> Void,
@manas-sharma-1683
manas-sharma-1683 / access_catalogs.py
Last active February 18, 2021 19:44
How to access asset catalogs from your Xcode project.
# !/usr/bin/python3
"""
Asset catalogs are not accessible in your Xcode project because they are compiled archive (.car files) so doing
something like Bundle.main.url().. won't work.
"""
"""
1. Manually add a file colors.json to your Xcode project on the level ${PROJECT_DIR}/project_name/<here> and
@manas-sharma-1683
manas-sharma-1683 / future_init.swift
Created February 9, 2021 21:16
Future initializer that can accept throwing expressions.
import Combine
import Foundation
extension Future where Failure == Error {
convenience init(promise: @escaping (@escaping (Result<Output, Failure>) -> Void) throws -> Void) {
self.init({ (innerPromise) in
do {
try promise { innerPromise($0) }
} catch {
innerPromise(.failure(error))
@manas-sharma-1683
manas-sharma-1683 / result_sink.swift
Last active March 3, 2021 08:58
Combine + Result.
/*
*
* Use this when the publisher only publishes 1 value then stops.
*
*/
import Combine
import Foundation
@manas-sharma-1683
manas-sharma-1683 / uiimage_codable.swift
Created February 18, 2021 20:54
UIImage+Codable.swift
extension UIImage: Codable {
// works
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(pngData()!)
}
@manas-sharma-1683
manas-sharma-1683 / line_deletion.swift
Last active February 25, 2021 11:48
Detect UITextView line deletion.
import UIKit
class ViewController: UIViewController {
private let textView = UITextView()
private var beforeNumberOfLines: Int?
override func viewDidLoad() {
super.viewDidLoad()
@manas-sharma-1683
manas-sharma-1683 / or_throw.swift
Created February 25, 2021 22:51
??? operator in swift.
infix operator ???
func ???<T> (_ value: @autoclosure () -> T?, _ error: @autoclosure () -> Error) throws -> T {
guard let value = value() else {
throw error()
}
return value
}
@manas-sharma-1683
manas-sharma-1683 / defaults.swift
Created March 1, 2021 21:11
Type safe User defaults.
import Foundation
@propertyWrapper
struct Defaults<Value> {
let key: Key
let defaultValue: Value
var wrappedValue: Value {
@manas-sharma-1683
manas-sharma-1683 / redactable.swift
Created March 1, 2021 21:14
Unified swift logging.
import os.log
import Foundation
@propertyWrapper
struct Redactable<Value: Codable & Hashable>: Codable, Hashable, CustomLeafReflectable,
CustomStringConvertible, CustomDebugStringConvertible, CustomPlaygroundDisplayConvertible {
private let value: Value
var isRedactionEnabled = true