Skip to content

Instantly share code, notes, and snippets.

View steipete's full-sized avatar

Peter Steinberger steipete

View GitHub Profile
extension Data {
init?(hexString: String) {
let count = hexString.count / 2
var data = Data(capacity: count)
var i = hexString.startIndex
for _ in 0 ..< count {
let j = hexString.index(after: i)
if var byte = UInt8(hexString[i ... j], radix: 16) {
data.append(&byte, count: 1)
} else {
//
// EmptySwiftUIModifiers.swift
// Architectory
//
// Created by Nikita Patskov on 09.09.2020.
//
import SwiftUI
struct TestView: View {
@arroz
arroz / gist:1d2e8bc9b3103a5e903a7786c6a11534
Created March 30, 2021 15:24
Concurrent queue issue
let queue = DispatchQueue(label: "Queue", qos: .default, attributes: [.concurrent], autoreleaseFrequency: .workItem, target: .global(qos: .default))
let cancellable = [1, 2, 3, 4, 5].publisher
.receive(on: queue)
.map { longRunningFunc(value: $0) }
.receive(on: DispatchQueue.main)
.sink (receiveCompletion: { completion in
Swift.print(completion)
}, receiveValue: { value in
Swift.print(value)
@schwa
schwa / environment.swift
Created March 27, 2021 15:49
Dash Snippet for SwiftUI Environments & Modifiers
struct __ValueType__ {
}
struct __ValueType__Key: EnvironmentKey {
static var defaultValue = __ValueType__()
}
extension EnvironmentValues {
var __KeyName__: __ValueType__ {
get {
extension Result {
public func `catch`(_ handler: () throws -> Success) -> Result<Success, Error> {
flatMapError { _ in
.init { try handler() }
}
}
public func `catch`(_ handler: (Failure) throws -> Success) -> Result<Success, Error> {
flatMapError { error in
.init { try handler(error) }
// Copyright © 2020 Michael Gubik. All rights reserved. Created on 2020-05-16.
import Foundation
#if TEST
import Sentry
#endif
// maybe look into os_log: https://stackoverflow.com/a/25951564/11588848
private enum LogLevel: String {
@Amzd
Amzd / ColorPicker.swift
Last active January 25, 2025 08:49
What SwiftUI's ColorPicker should have been.
import SwiftUI
@available(iOS 14.0, *)
public struct ColorPickerWithoutLabel: UIViewRepresentable {
@Binding var selection: Color
var supportsAlpha: Bool = true
public init(selection: Binding<Color>, supportsAlpha: Bool = true) {
self._selection = selection
self.supportsAlpha = supportsAlpha
struct ContentView: View {
var body: some View {
Image(nsImage: NSImage(named: .init(NSImage.applicationIconName))!)
.resizable()
.frame(width: 100, height: 100)
.tapWithHighlight(onTap: {
print("Tap")
}, onLongPress: {
print("Long Press")
})
@rnystrom
rnystrom / example.swift
Created January 18, 2021 00:30
Show the keyboard as a UISearchController in a modal is displayed
class ViewController: UIViewController {
@IBAction func onSearch(_ sender: Any) {
let fakeWindow = UIWindow(windowScene: view.window!.windowScene!)
let fakeTextField = UITextField()
fakeTextField.autocorrectionType = .no
fakeWindow.addSubview(fakeTextField)
fakeWindow.makeKeyAndVisible()
fakeTextField.becomeFirstResponder()
@DougGregor
DougGregor / SwiftConcurrencyDependencies.svg
Created December 2, 2020 00:39
Swift Concurrency Proposal Dependencies
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.