Skip to content

Instantly share code, notes, and snippets.

View daniyalyousuf07's full-sized avatar
🎯
Focusing

Daniyal daniyalyousuf07

🎯
Focusing
View GitHub Profile
@daniyalyousuf07
daniyalyousuf07 / Vehicle.swift
Created March 8, 2025 06:26
Gist to demonstrate the concept of borrowing in non-copyable value types
struct Car {
private var maxSpeed: Int = 10
mutating func increaseLimit() {
maxSpeed += 10
}
}
struct VehicleManager: ~Copyable {
@daniyalyousuf07
daniyalyousuf07 / SecretMessage.swift
Created March 7, 2025 05:26
Using ~copyable to demonstrate non-copyable values
struct SpecialMessage: ~Copyable {
private var transmitterId: String
private var cipher: String
init(transmitterId: String, cipher: String) {
self.transmitterId = transmitterId
self.cipher = cipher
}
//one time decryption
@daniyalyousuf07
daniyalyousuf07 / Safedictionary.swift
Last active November 14, 2024 04:08
SafedictionaryMutexExampleiOS18
import Foundation
import Synchronization
final class SafeDictionary {
static let shared = SafeDictionary()
private init() {}
//
// ContentView.swift
// ActorSample
//
// Created by Daniyal Yousuf
//
import SwiftUI
//no need to use nslock or dispatchqueue or other locking as we would do in case of a class
//
// ContentView.swift
// SendableProtocol
//
// Created by Daniyal Yousuf
//
import SwiftUI
//thread safe class before being passed to an Actor
@daniyalyousuf07
daniyalyousuf07 / CarComponents.swift
Last active May 14, 2024 04:51
Composite Pattern
protocol CarComponent: AnyObject {
var name: String { get }
func getPrice() -> Double
func displayDetails()
}
final class Part: CarComponent {
let name: String
private let price: Double
init(name: String, price: Double) {
@daniyalyousuf07
daniyalyousuf07 / SE0255.swift
Created February 3, 2024 06:57
If vs Switch expressions
//if vs switch expressions //Swift 5.9
enum Status {
case Pass
case Fail
case Merit
}
let result = if (Int.random(in: 0...100)) > 50 { Status.Pass } else { Status.Fail }
@daniyalyousuf07
daniyalyousuf07 / PulsationView.swift
Created September 1, 2023 03:29
SwiftUI - PulsationView
struct PulsationView: View {
// MARK: - PROPERTIES
@State private var animation: Double = 0.0
// MARK: - BODY
var gradientColor = LinearGradient(gradient: Gradient(colors: [.green, .yellow]),
startPoint: .top,
endPoint: .bottom)
@daniyalyousuf07
daniyalyousuf07 / Concurrency.swift
Created June 16, 2023 04:13
This class shows the working of a serial and a concurrent queue with synchronous and asynchronous tasks submitted.
import Foundation
///////Points to Remember
//1 - serial - only 1 thread
//2 - concurrent - multiple threads
//3 - async tasks - tasks can be submitted to either a serial queue or a concurrent queue.
//4- sync tasks - tasks can be submitted to either a serial queue or a concurrent queue.
//5 - If sync tasks are submitted to either a serial queue or a concurrent, only 1 task will execute at a time because of its synchronous nature.
//6 - if async tasks are submitted to a concurrent queue, multiple tasks will execute at a time because of their asynchronous nature. But we can limit the number of tasks to be executed using DispatchSemaphore
@daniyalyousuf07
daniyalyousuf07 / FruitCart.swift
Created May 24, 2023 05:33
Understanding when to use weak references versus unowned references is illustrated with an example.
protocol FruitCart {
var price: Double? { get }
func checkPrice()
}
class FruitExpenses {
var fruit: FruitCart?
func checkPrice(fruit: FruitCart) {
self.fruit = fruit
fruit.checkPrice()