Skip to content

Instantly share code, notes, and snippets.

@m4rr
Last active March 27, 2025 11:22
Show Gist options
  • Save m4rr/7fe9d3e6cf35cf1470bc1a77ab6cc324 to your computer and use it in GitHub Desktop.
Save m4rr/7fe9d3e6cf35cf1470bc1a77ab6cc324 to your computer and use it in GitHub Desktop.
Swift kit
private extension SwiftUI.Text {
init?(_ strOrNil: String?) {
if let strOrNil {
self.init(strOrNil)
} else {
return nil
}
}
}
extension Numeric {
// @available(*, deprecated, message: "Use += instead!")
static prefix func ++ (value: inout Self) -> Self {
value += 1
return value
}
// @available(*, deprecated, message: "Use -= instead!")
static prefix func -- (value: inout Self) -> Self {
value -= 1
return value
}
// @available(*, deprecated, message: "Use something else!")
static postfix func ++ (value: inout Self) -> Self {
defer { value += 1 }
return value
}
// @available(*, deprecated, message: "Use something else!")
static postfix func -- (value: inout Self) -> Self {
defer { value -= 1 }
return value
}
}
extension StringProtocol {
subscript(_ offset: Int) -> Element {
self[index(startIndex, offsetBy: offset)]
}
subscript(_ range: Range<Int>) -> SubSequence {
prefix(range.lowerBound+range.count).suffix(range.count)
}
subscript(_ range: ClosedRange<Int>) -> SubSequence {
prefix(range.lowerBound+range.count).suffix(range.count)
}
subscript(_ range: PartialRangeThrough<Int>) -> SubSequence {
prefix(range.upperBound.advanced(by: 1))
}
subscript(_ range: PartialRangeUpTo<Int>) -> SubSequence {
prefix(range.upperBound)
}
subscript(_ range: PartialRangeFrom<Int>) -> SubSequence {
suffix(Swift.max(0, count-range.lowerBound))
}
}
extension LosslessStringConvertible {
var string: String { .init(self) }
}
extension BidirectionalCollection {
subscript(safe offset: Int) -> Element? {
guard !isEmpty, let i = index(startIndex, offsetBy: offset, limitedBy: index(before: endIndex)) else { return nil }
return self[i]
}
}
extension AnyTransition {
static var moveAndFade: AnyTransition {
let insertion = AnyTransition.move(edge: .trailing)
.combined(with: .opacity)
let removal = AnyTransition.scale
.combined(with: .opacity)
return .asymmetric(insertion: insertion, removal: removal)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment