Skip to content

Instantly share code, notes, and snippets.

View gopalkrishnareddy's full-sized avatar

Gopal Krishna Reddy Thotli gopalkrishnareddy

  • Harman India
  • Bangalore
View GitHub Profile
extension StringProtocol {
subscript(_ offset: Int) -> String.Element {
if offset >= 0 {
self[index(startIndex, offsetBy: offset)]
} else {
self[index(endIndex, offsetBy: offset)]
}
}
@pchelnikov
pchelnikov / URLRequest-debugging.swift
Last active December 8, 2023 11:16
URLRequest debugging
//Declare extension to URLRequest:
extension URLRequest {
public var curlString: String {
// Logging URL requests in whole may expose sensitive data,
// or open up possibility for getting access to your user data,
// so make sure to disable this feature for production builds!
#if !DEBUG
return ""
@stephancasas
stephancasas / NSApplication+openSettings.swift
Last active December 5, 2023 03:22
An extension enabling global access to settings scene of a macOS SwiftUI application.
//
// NSApplication+openSettings.swift
//
// Created by Stephan Casas on 12/3/23.
//
import SwiftUI;
fileprivate let kAppMenuInternalIdentifier = "app"
fileprivate let kSettingsLocalizedStringKey = "Settings\\U2026";
@lukepistrol
lukepistrol / AsyncButton.swift
Created October 17, 2022 21:00
A SwiftUI Button that can perform asyncronous tasks.
import SwiftUI
/// A control that initiates an action asyncronously.
///
/// You create a button by providing an action and a label.
/// The action is either a method or closure property that
/// does something when a user clicks or taps the button.
/// The label is a view that describes the button’s action —
/// for example, by showing text, an icon, or both:
///
@lukepistrol
lukepistrol / TaskTrigger.swift
Last active November 19, 2023 19:32
Attach async tasks to SwiftUI views using a trigger mechanism.
import SwiftUI
struct TaskTrigger<T: Equatable>: Equatable {
fileprivate enum TaskState<S: Equatable>: Equatable {
case inactive
case active(value: S, uniqueId: UUID? = nil)
}
fileprivate var state: TaskState<T> = .inactive
@Amzd
Amzd / DictionaryKeyPath.swift
Last active November 5, 2023 04:46 — forked from casperzandbergenyaacomm/DictionaryKeyPath.swift
Reading and writing to (possible) nested dictionaries for a given key path, using a recursive approach
// Inspired by: https://gist.github.com/dfrib/d7419038f7e680d3f268750d63f0dfae
import Foundation
public extension Dictionary {
subscript(keyPath string: Key, separator: String) -> Value? where Key == String {
get { return self[keyPath: string.components(separatedBy: separator)] }
set { self[keyPath: string.components(separatedBy: separator)] = newValue }
}
@ericdke
ericdke / getMacUUID.swift
Last active October 31, 2023 05:04
Swift: get the Mac UUID
func getSystemUUID() -> String? {
let dev = IOServiceMatching("IOPlatformExpertDevice")
let platformExpert: io_service_t = IOServiceGetMatchingService(kIOMasterPortDefault, dev)
let serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformUUIDKey, kCFAllocatorDefault, 0)
IOObjectRelease(platformExpert)
let ser: CFTypeRef = serialNumberAsCFString.takeUnretainedValue()
if let result = ser as? String {
return result
}
return nil
@Amzd
Amzd / PreferenceUIHostingController.swift
Last active September 8, 2023 12:14
PreferenceUIHostingController. Adds hiding home indicator and deferring system edge gestures to SwiftUI. (Don't work at the same time but I think that's normal?)
extension View {
/// Controls the application's preferred home indicator auto-hiding when this view is shown.
func prefersHomeIndicatorAutoHidden(_ value: Bool) -> some View {
preference(key: PreferenceUIHostingController.PrefersHomeIndicatorAutoHiddenPreferenceKey.self, value: value)
}
/// Controls the application's preferred screen edges deferring system gestures when this view is shown. Default is UIRectEdgeNone.
func edgesDeferringSystemGestures(_ edge: UIRectEdge) -> some View {
preference(key: PreferenceUIHostingController.PreferredScreenEdgesDeferringSystemGesturesPreferenceKey.self, value: edge)
}
@ollieatkinson
ollieatkinson / Restorable.swift
Created June 16, 2020 08:43
Restorable - Undo/Redo management of values using Swift 5.1 property wrappers
@propertyWrapper
public struct Restorable<Value> {
public var wrappedValue: Value
public init(wrappedValue: Value, using undoManager: UndoManager = .init()) {
self.wrappedValue = wrappedValue
self.projectedValue = undoManager
}
@EnesKaraosman
EnesKaraosman / RandomColor.swift
Last active July 14, 2023 09:35
Generatin random color in SwiftUI & UIKit
#if canImport(UIKit)
import UIKit
extension UIColor {
static var random: UIColor {
return UIColor(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)