This file contains 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 View { | |
func debugPrint(_ value: Any) -> some View { | |
#if DEBUG | |
print (value) | |
#endif | |
return self | |
} | |
// Example usage | |
struct ContentView: View { |
This file contains 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
func incr(_ x: Int) -> Int { | |
return x + 1 | |
} | |
func square(_ x: Int) -> Int { | |
return x * x | |
} | |
precedencegroup ForwardApplication { | |
associativity: left |
This file contains 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
// Author: SwiftUI-Lab (www.swiftui-lab.com) | |
// Description: This code is part of the "Advanced SwiftUI Animations - Part 5" | |
// Article: https://swiftui-lab.com/swiftui-animations-part5/ | |
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
ClockView() | |
.background(.gray) |
This file contains 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
struct FuzzyTextSearch { | |
private static let maxMatches = 256 | |
private static let defaultMatches: [Int] = Array(repeating: -1, count: maxMatches) | |
private static let recursionLimit = 10 | |
private static let sequentialBonus = 15 // bonus for adjacent matches | |
private static let separatorBonus = 30 // bonus if match occurs after a separator | |
private static let camelBonus = 30 // bonus if match is uppercase and prev is lower | |
private static let firstLetterBonus = 15 // bonus if the first letter is matched | |
private static let leadingLetterPenalty = -5 // penalty applied for every letter in str before the first match | |
private static let maxLeadingLetterPenalty = -15 // maximum penalty for leading letters |
This file contains 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
struct ChildSizeReader<Content: View>: View { | |
@Binding var size: CGSize | |
let content: () -> Content | |
var body: some View { | |
ZStack { | |
content() | |
.background( | |
GeometryReader { proxy in | |
Color.clear | |
.preference(key: SizePreferenceKey.self, value: proxy.size) |
This file contains 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 SwiftUI | |
struct ContentView: View { | |
@State private var rating = 0.0 | |
var body: some View { | |
Slider(value: $rating.onChange(sliderChanged)) | |
} | |
func sliderChanged(_ value: Double) { |
This file contains 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
//Like we mentioned earlier, different locales separate decimals and thousands with a different symbol. Most English people will use commas for thousands, whereas in Spanish speaking countries we normally separate with periods. | |
//This can be done correctly with NumberFormatter. | |
let numFormatter = NumberFormatter() | |
numFormatter.numberStyle = .decimal | |
numFormatter.string(from: 32.823) // 32.823 (English), 32,823 (Spanish) |
This file contains 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
//We can also format lists. Suppose you want to say “Apples, eggs, and pears” when you have the following array: | |
let items = ["apples", "eggs", "pears"] | |
//You can probably think of a clever way to concatenate the first two items only with a comma and then follow the last item with the word “and”. In fact doing so may not be complicated, but we have ListFormatter which allows us to do just that in just one line of code: | |
let items = ["apples", "eggs", "pears"] | |
//ListFormatter.localizedString(byJoining: items) // apples, eggs, and pears | |
//If you remove one of the elements, the formatter will do the right thing: | |
let items = ["apples", "pears"] |
This file contains 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 dateFormatter = DateFormatter() | |
dateFormatter.locale = Locale(identifier: "en_US") | |
dateFormatter.setLocalizedDateFormatFromTemplate("MMddyyyy") | |
dateFormatter.string(from: Date()) // US: 09/27/2020 | |
// ... | |
dateFormatter.locale = Locale(identifier: "es_BO") | |
dateFormatter.setLocalizedDateFormatFromTemplate("MMddyyyy") | |
dateFormatter.string(from: Date()) // Bolivia: 27/09/2020 |
NewerOlder