This file contains hidden or 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
| /* | |
| * @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( |
This file contains hidden or 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
| 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?() | |
| } |
This file contains hidden or 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
| // | |
| // CADisplayLinkPublisher.swift | |
| // SwiftUIDemo | |
| // | |
| // Created by Ahmed Khalaf on 8/19/20. | |
| // | |
| // Credits: https://www.avanderlee.com/swift/custom-combine-publisher/ | |
| import UIKit | |
| import Combine |
This file contains hidden or 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
| @propertyWrapper | |
| struct GetOnlyOptional<T> { | |
| init(wrappedValue: T?) { | |
| self.wrappedValue = wrappedValue | |
| } | |
| var wrappedValue: T? { | |
| get { | |
| _value | |
| } | |
This file contains hidden or 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
| class ProgressManager { | |
| init(progressStore: ProgressStoring) { | |
| self.progressStore = progressStore | |
| self.progressStore.didReceiveNewProgress = { [weak self] progress in | |
| guard let self = self else { return } | |
| self.progress = progress | |
| } | |
| } | |
This file contains hidden or 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
| 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)!)) | |
| } | |
| } |
This file contains hidden or 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 UIStackView { | |
| var padding: NSDirectionalEdgeInsets { | |
| get { | |
| isLayoutMarginsRelativeArrangement ? directionalLayoutMargins : .zero | |
| } | |
| set { | |
| isLayoutMarginsRelativeArrangement = true |
This file contains hidden or 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
| 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() { |
This file contains hidden or 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
| 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]) |
This file contains hidden or 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
| extension Numeric where Self: Comparable { | |
| func clamped(byMin min: Self, max: Self) -> Self { | |
| return Swift.min(Swift.max(min, self), max) | |
| } | |
| } |
NewerOlder