Skip to content

Instantly share code, notes, and snippets.

@ahmedk92
ahmedk92 / Solution.swift
Created August 21, 2021 21:17
Rectangle Area
/*
* @lc app=leetcode id=223 lang=swift
*
* [223] Rectangle Area
*/
// @lc code=start
class Solution {
func computeArea(_ ax1: Int, _ ay1: Int, _ ax2: Int, _ ay2: Int, _ bx1: Int, _ by1: Int, _ bx2: Int, _ by2: Int) -> Int {
let rect1 = CGRect(
class ViewModel {
private let repository: Repository
private(set) var data: Data?
var updateUi: (() -> Void)?
func viewDidLoad() {
repository.getData(cachePolicy: .cacheThenNetwork) { [weak self] data in
self?.data = data
self?.updateUi?()
}
@ahmedk92
ahmedk92 / CADisplayLinkPublisher.swift
Created August 19, 2020 09:49
Combine Publisher for CADisplayLink
//
// CADisplayLinkPublisher.swift
// SwiftUIDemo
//
// Created by Ahmed Khalaf on 8/19/20.
//
// Credits: https://www.avanderlee.com/swift/custom-combine-publisher/
import UIKit
import Combine
@ahmedk92
ahmedk92 / GetOnlyOptional.swift
Created June 24, 2020 21:24
A property wrapper for values that can be initially nil, but once it has a value, it cannot be reset to nil afterwards.
@propertyWrapper
struct GetOnlyOptional<T> {
init(wrappedValue: T?) {
self.wrappedValue = wrappedValue
}
var wrappedValue: T? {
get {
_value
}
@ahmedk92
ahmedk92 / Demo.swift
Created June 22, 2020 16:29
Simultaneous accesses to X, but modification requires exclusive access.
class ProgressManager {
init(progressStore: ProgressStoring) {
self.progressStore = progressStore
self.progressStore.didReceiveNewProgress = { [weak self] progress in
guard let self = self else { return }
self.progress = progress
}
}
@ahmedk92
ahmedk92 / print_glyphnames.swift
Created June 5, 2020 22:28
Print Glyph Names
let font = CGFont("fontName" as CFString)!
for glyphIndex in 0..<font.numberOfGlyphs {
let hexPart = (font.name(for: CGGlyph(glyphIndex))! as String).replacingOccurrences(of: "uni", with: "")
if let int = Int(hexPart, radix: 16) {
print(Character(Unicode.Scalar(int)!))
}
}
@ahmedk92
ahmedk92 / UIStackView+Padding.swift
Last active June 3, 2020 13:14
Add padding to UIStackView
import UIKit
extension UIStackView {
var padding: NSDirectionalEdgeInsets {
get {
isLayoutMarginsRelativeArrangement ? directionalLayoutMargins : .zero
}
set {
isLayoutMarginsRelativeArrangement = true
@ahmedk92
ahmedk92 / ParagraphsNaturallyAligned.swift
Last active May 1, 2020 04:42
Aligns paragraphs according to each's dominant language.
protocol ParagraphsNaturallyAligned: NSAttributedString {}
extension ParagraphsNaturallyAligned {
var paragraphsNaturallyAligned: Self {
let mutable = NSMutableAttributedString(attributedString: self)
mutable.alignParagraphsNaturally()
return (self is NSMutableAttributedString ? mutable : NSAttributedString(attributedString: mutable)) as! Self
}
}
extension ParagraphsNaturallyAligned where Self == NSMutableAttributedString {
func alignParagraphsNaturally() {
extension String {
var digitsArabized: String {
do {
let regex = try NSRegularExpression(pattern: #"\d"#, options: [])
let arabicDigits = Array("٠١٢٣٤٥٦٧٨٩")
var mutableSelf = self
regex.matches(in: self, options: [], range: nsRange).reversed().forEach {
guard
let range = Range($0.range, in: self),
let digit = Int(self[range])
extension Numeric where Self: Comparable {
func clamped(byMin min: Self, max: Self) -> Self {
return Swift.min(Swift.max(min, self), max)
}
}