Skip to content

Instantly share code, notes, and snippets.

View Mr-Alirezaa's full-sized avatar
👨‍💻

Alireza Asadi Mr-Alirezaa

👨‍💻
View GitHub Profile
@Mr-Alirezaa
Mr-Alirezaa / DebugPrinterView.swift
Created November 29, 2022 22:58
DebugPrinterView may be used to print expressions into Xcode canvas (just as print function writes to Xcode console)
import SwiftUI
import Foundation
@resultBuilder
struct DebugPrinterContentBuilder {
static func convertItems(_ items: [Any]) -> [String] {
items.map { item in
var buffer: String = ""
print(item, terminator: "", to: &buffer)
return buffer
@Mr-Alirezaa
Mr-Alirezaa / CachingWithActorExample.swift
Last active October 30, 2022 07:37
This is an example of using swift actor type to handle downloading of images from URLs and avoid problems caused by "Actor Reentrancy".
// See: https://stackoverflow.com/a/70595187/8249180
actor ImageDownloader {
private enum CacheEntry {
case inProgress(Task<Image, Error>)
case ready(Image)
}
private var cache: [URL: CacheEntry] = [:]
@Mr-Alirezaa
Mr-Alirezaa / User Agent.swift
Last active July 29, 2019 05:00
This piece of code generates a User-Agent string based on the app name, os and arc. If you are using xcode you will see a placeholder instead of <#Your Class Name#>. Replace it with a name of a class in your bundle.
let userAgent: String = {
var components: [String] = []
if let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String ?? Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String {
let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
components.append("\(appName)/\(version)")
}
// Replace Your bundle name
let libraryBundle: Bundle? = Bundle(for: <#Your Class Name#>.self)