Skip to content

Instantly share code, notes, and snippets.

View d4rkd3v1l's full-sized avatar
😈
1337

Martin Straub d4rkd3v1l

😈
1337
View GitHub Profile
@d4rkd3v1l
d4rkd3v1l / URLRequest+raw.swift
Created October 27, 2021 13:03
Print URLRequests as "raw" Strings in Swift
extension URLRequest {
/// Prints the request as "raw" String
///
/// Like:
/// ```
/// GET /path HTTP/1.1
/// Host: host
/// Header: Value
/// Header: Value
///
@d4rkd3v1l
d4rkd3v1l / ExpansionHandler.swift
Last active January 31, 2024 02:24
SwiftUI: DisclosureGroup "expansion handler" to only have one group expanded at a time, and automatically hiding the last one, when expanding a new one.
class ExpansionHandler<T: Equatable>: ObservableObject {
@Published private (set) var expandedItem: T?
func isExpanded(_ item: T) -> Binding<Bool> {
return Binding(
get: { item == self.expandedItem },
set: { self.expandedItem = $0 == true ? item : nil }
)
}
@d4rkd3v1l
d4rkd3v1l / Struct+StoreOnHeap.swift
Last active September 13, 2022 06:34
Swift @propertyWrappers to allocate huge structs on the heap, by wrapping them inside arrays. -> Very effective "hackaround" 😬😎
/// Stores value types on the heap
///
/// Arrays are stored on the heap and only the pointer (8 bytes) to the array remains on the stack.
/// This behaviour is used to wrap another type into an array, and therefore move it to the heap.
///
/// Example usage:
/// ```
/// @StoredOnHeap var hugeStruct: HugeStruct
/// ```
@propertyWrapper
@d4rkd3v1l
d4rkd3v1l / Struct+StoreOnHeap.swift
Created May 28, 2020 09:50
Swift @propertyWrapper to allocate huge structs on the heap, by wrapping them inside arrays. -> Very effective "hackaround".
/// Stores value types on the heap
///
/// Arrays are stored on the heap and only the pointer (8 bytes) to the array remains on the stack.
/// This behaviour is used to wrap another type into an array, and therefore move it to the heap.
///
/// Example usage:
/// ```
/// @StoredOnHeap var hugeStruct: HugeStruct
/// ```
@propertyWrapper
@d4rkd3v1l
d4rkd3v1l / Image.m
Last active November 29, 2017 14:33
iOS: How to retrieve image dimensions without loading CGImage into memory
// This method requires ImageIO.framework
#import <ImageIO/ImageIO.h>
- (CGSize)sizeOfImageAtURL:(NSURL *)imageURL
{
// With CGImageSource we avoid loading the whole image into memory
CGSize imageSize = CGSizeZero;
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)imageURL, NULL);
if (source) {
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCGImageSourceShouldCache];
@d4rkd3v1l
d4rkd3v1l / git-reset.sh
Last active February 5, 2016 17:39
Reset stuck files in git (complete reset)
#!/bin/sh
# execute this in your repo's root dir
git rm --cached -r .
git reset --hard