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 / autolayout_logs.swift
Created March 5, 2021 12:44
Turn off auto layout logs.
func turnOffAutoLayoutLogs() {
UserDefaults.standard.setValue(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable")
}
@manas-sharma-1683
manas-sharma-1683 / async_attachment.swift
Created March 1, 2021 21:16
Load remote images in NSTextAttachment.
/*
*
* If you're using this in UITextView, setting isSelectable or isEditable properties to false will cause the image to not load,
* instead set dataDetectorTypes to an empty array if you don't want user to interact with the text view.
*
*/
import UIKit
@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
@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 / 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 / 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 / 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 / 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 / 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 / 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