Skip to content

Instantly share code, notes, and snippets.

View tatimagdalena's full-sized avatar

Tatiana Magdalena tatimagdalena

View GitHub Profile
public extension Bool {
var not: Bool { !self }
}
// zip
public extension Optional {
static func zip<B>(
_ a: Wrapped?,
_ b: B?
private extension Array {
func chunked(into size: Int) -> [[Element]] {
stride(from: 0, to: count, by: size).map {
Array(self[$0 ..< Swift.min($0 + size, count)])
}
}
static func += (lhs: inout Self, rhs: Self.Element) {
lhs.append(rhs)
}
// MARK: Optional
public extension Optional where Wrapped == String {
var orEmpty: String { self ?? "" }
var isNilOrEmpty: Bool {
orEmpty.isEmpty
}
var isNotNilNorEmpty: Bool {
private func filter(searchText: String, using keyPath: KeyPath<Object, String>) -> [Object] {
originalList.filter {
let value = $0[keyPath: keyPath]
return value == searchText
}
}
private func filter(searchText: String, using matcher: @escaping (Object, String) -> Bool) -> [Object] {
originalList.filter { object in
matcher(object, searchText)
@tatimagdalena
tatimagdalena / VersionComparison.swift
Created March 19, 2019 18:44
Comparing two version strings in the format "n.n.n" to see if the first one is outdated
static func isOutdated(thisVersion: String, comparisonVersion: String) -> Bool {
let thisVersionComponents = thisVersion.components(separatedBy: ".")
let comparisonVersionComponents = comparisonVersion.components(separatedBy: ".")
let thisVersionNumbers: [Int] = thisVersionComponents.map { Int($0) ?? 0 }
let comparisonVersionNumbers: [Int] = comparisonVersionComponents.map { Int($0) ?? 0 }
for i in 0 ..< 3 {
if thisVersionNumbers[i] > comparisonVersionNumbers[i] {
@tatimagdalena
tatimagdalena / Helpful-articles.txt
Created March 14, 2018 17:50
Some helpful articles
--------------
- Rx testing:
--------------
https://github.com/ReactiveX/RxSwift/blob/master/Documentation/UnitTests.md
http://rx-marin.com/post/rxswift-rxtests-unit-tests/
http://rx-marin.com/post/rxblocking-part1/
http://adamborek.com/rxtests-rxactionsheet/
@tatimagdalena
tatimagdalena / DateComparison.swift
Last active June 5, 2023 06:41
Compare two dates depending on granularity.
let granularity = Calendar.Component.day
let comparisonResult = Calendar.current.compare(firstDate, to: secondDate, toGranularity: granularity)
// granularity:
// .second
// .minute
// .hour
// .day
// .month
// .year
@tatimagdalena
tatimagdalena / ViewController.swift
Created March 8, 2017 12:38
Dismiss keyboard when touching outside
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
}
}
@tatimagdalena
tatimagdalena / String+CaseTransform.swift
Last active October 19, 2023 16:02
String extension to transform string into several case types
import Foundation
extension String {
public var upperCasedFirstLetter: String {
prefix(1).uppercased() + dropFirst()
}
public var lowerCasedFirstLetter: String {
prefix(1).lowercased() + dropFirst()
func convertToValidInt(amount: String) -> Int?
if amount != "" {
let badCharacters = CharacterSet.decimalDigits.inverted
if amount.rangeOfCharacter(from: badCharacters) == nil {
if let amount = Int(amount) {
if amount > 0 {
return amount
}
}
}