Skip to content

Instantly share code, notes, and snippets.

@hirad
hirad / Persistence.swift
Created June 1, 2020 19:44
Compiler crasher
import Foundation
import Combine
private let docsDirURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
@propertyWrapper
struct Persisted<Value: Codable> {
let fileName: String
let defaultValue: Value
@hirad
hirad / FixXcode.scpt
Created February 17, 2017 22:18
AppleScript to clean Xcode project, quit it, nuke derived data, re-open it and build
tell application "Xcode"
set workspaceFile to file of active workspace document
tell active workspace document
set res to (clean it)
repeat
if completed of res is true then
exit repeat
end if
delay 1.0
end repeat
@hirad
hirad / ObjCNilGuard.h
Last active December 2, 2015 22:45
Objective-C Guard Macro For nil Objects
#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define FOR_EACH_1(_what_, _separator_, X) _what_(X)
#define FOR_EACH_2(_what_, _separator_, X, ...) _what_(X) _separator_ FOR_EACH_1(_what_, _separator_, __VA_ARGS__)
#define FOR_EACH_3(_what_, _separator_, X, ...) _what_(X) _separator_ FOR_EACH_2(_what_, _separator_, __VA_ARGS__)
#define FOR_EACH_4(_what_, _separator_, X, ...) _what_(X) _separator_ FOR_EACH_3(_what_, _separator_, __VA_ARGS__)
#define FOR_EACH_SEP(_action_, _separator_, ...) GET_MACRO(__VA_ARGS__, FOR_EACH_4, FOR_EACH_3, FOR_EACH_2, FOR_EACH_1)(_action_, _separator_, __VA_ARGS__)
#define FOR_EACH(_action_, ...) FOR_EACH_SEP(_action_, ;, __VA_ARGS__)
@hirad
hirad / EasyArchives.m
Last active October 16, 2015 21:22
Fooling around with the ObjC Runtime
@interface ArchivesEasy: NSObject <NSCoding>
@end
@implementation ArchivesEasy
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
[self enumeratePropertiesWithBlock:^(NSString *key, id value) {
[self setValue:[aDecoder decodeObjectForKey:key] forKey:key];
@hirad
hirad / AsyncOperation.swift
Last active October 9, 2015 21:49
A simple NSOperation subclass to make creating concurrent operations easier
public class AsyncOperation: NSOperation {
enum State {
case Pending
case Executing
case Finished
case Cancelled
var keyValueCodingKeys: [String] {
switch self {
@hirad
hirad / CoreDataBasics.m
Last active August 29, 2015 14:25
Core Data Basics
// Setting up the stack
NSString* libraryDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString* storePath = [libraryDir stringByAppendingPathComponent:@"store.sqlite"];
NSURL* storeURL = [NSURL fileURLWithPath:storePath];
NSURL* momURL = [[NSBundle mainBundle] URLForResource:@"EmploymentModel" withExtension:@"momd"];
NSManagedObjectModel* mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
NSPersistentStoreCoordinator* psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSDictionary* storeOptions = @{NSMigratePersistentStoresAutomaticallyOption: @YES,
@hirad
hirad / W2D2 In Class
Created July 8, 2015 00:47
LHL W2D2 In Class Example
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSError* error = nil;
NSString* textString = [NSString stringWithContentsOfFile:@"/path/to/some/file.txt"
encoding:NSUTF8StringEncoding
error:&error];
if (textString == nil) {
NSLog(@"Something went wrong with reading the file: %@", error);
}
@hirad
hirad / RPNSolver
Last active April 29, 2017 18:03
A Simple RPN Calculator in Swift
struct Stack<T> {
let values: [T]
init(_ v: [T] = [T]()) {
self.values = v
}
func push(elem: T) -> Stack<T> {
let newValues = [elem] + self.values
return Stack<T>(newValues)
@hirad
hirad / SwiftComprehension
Created May 4, 2015 18:21
Simple List Comprehensions in Swift
infix operator <- { associativity right precedence 140 }
infix operator |? { associativity right precedence 140 }
func |? <S>(source: [S], predicates: [(s: S) -> Bool]) -> [S] {
var ps = predicates
return count(ps) > 0 ? (filter(source, ps.removeLast())) |? ps : source
}
func <-<S, T>(transform: (s: S) -> T, source: [S]) -> [T] {
return map(source, transform)
@hirad
hirad / simpleRuntime
Last active August 29, 2015 14:10
Simple Objective-C runtime examples
@interface ArchivesEasy : NSObject
@end
@implementation ArchivesEasy
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
[self enumeratePropertiesWithBlock:^(NSString *key, id value) {
[self setValue:[aDecoder decodeObjectForKey:key] forKey:key];
}];