It's important to know the difference between static and dynamic dispatch for optimizing the code and better performance.
In this context, dispatching just refers to the action of finding the right function to call.
/* | |
There is a bug in SwiftUI, some how ignoreSafeArea(.top) don't work when you add PageStyle TabView inside NavigationStack. | |
This bug is reproduceable in minimum iOS 16.6 | |
Solution: Just add color to TabView. | |
*/ | |
struct ContentView: View { | |
var body: some View { | |
myTabView |
import Foundation | |
import SwiftUI | |
extension View { | |
@ViewBuilder | |
func shimmer(when isLoading: Bool) -> some View { | |
if isLoading { | |
self.modifier(Shimmer()) | |
.redacted(reason: .placeholder) |
func execute() { | |
let numberChannel = AsyncChannel<Void>() | |
let alphabetsChannel = AsyncChannel<Void>() | |
Task { | |
var iterator = (1...10).makeIterator() | |
for await _ in numberChannel { | |
if let number = iterator.next() { | |
print(number, terminator: " ") | |
await alphabetsChannel.send(()) |
Property Wrappers in Swift allows you to extract common logic in a distinct wrapper object.
When dealing with properties that represent some form of state, it is very common to have some kind of associated logic that gets triggered every time a value is modified. For example, we might validate each new value according to a set of rules, we might transform our assigned value in some way.
For example, let's say that we want to create a property wrapper that automatically capitalizes all String values that were assigned to it. That might be implemeneted like this.
"copy on write" is an optimization technique that helps in managing memory more efficiently.
It is used primarly with value types such as Array, Dictionary and String to defer the actual copying of data until it is modified. This helps in minimizing unnecessary data copying and thus improve performance.
When you assign or pass a value type to another variable or function. Swift doesn't immediately creates the copy of data. Instead it shares the same underlying storage until the copy is modified.
// Print all Divisors of a given Number | A10.swift | |
var arr = [Int]() | |
// x = sqr(n) | |
// TimeComlexity = O(x + xlog(x) + x) | |
for i in 1...n where i * i <= n { | |
if n % i == 0 { | |
arr.append(i) |
Brochette-case / Kebab case => Hello_World | |
lowerCamelCase / Dromedary Case => helloWorld | |
Pascal case / Upper Camal Case => HelloWorld | |
Snake case | |
Snake case starts with a lower case letter, and uses an underscore to separate words (although some variations start with an upper case). | |
Generally associated with the C programming language, although it actually started life with no particular name: | |
first + _ + Number = first_Number | |
Examples: my_func(), my_var, name, test |