Skip to content

Instantly share code, notes, and snippets.

#import <Foundation/Foundation.h>
#import <objc/runtime.h>
class Printer {
public:
const char *str;
~Printer() { puts(str); }
};
func effIt<Target, Result>(_ keypath: KeyPath<Target, Result>) -> (Target) -> Result {
return { $0[keyPath: keypath] }
}
struct Foo {
var x: Int
var y: Int
}
#import <Foundation/Foundation.h>
int main(int argc, char **argv) {
auto global = ^{ NSLog(@"Hello, world!"); };
auto captures = ^{ NSLog(@"argc is %d", argc); };
NSLog(@"global: %@ - captures: %@", global, captures);
NSLog(@"(void *)global: %p - (void *)captures: %p", (void *)global, (void *)captures);
id globalCopy = [global copy];
id capturesCopy = [captures copy];
func ??<T>(lhs: T?, rhs: @autoclosure () throws -> Never) rethrows -> T {
guard let value = lhs else { try rhs() }
return value
}
func ??<T>(lhs: T?, rhs: () throws -> Never) rethrows -> T {
guard let value = lhs else { try rhs() }
return value
}
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
Dl_info info;
int success = dladdr(main, &info);
if (success == 0) abort();
printf("main is at %p and the mach-o header that contains it is at %p\n", main, info.dli_fbase);
}
class C {}
protocol P where Self: C {
var x: Int { get }
var y: Int { get }
}
extension P {
var x: Int { return 42 }
}
#include <iostream>
#include <functional>
using namespace std;
void f(std::function<void(int)> call) {
call(1);
call(2);
call(3);
}
@import Foundation;
@import ObjectiveC;
static NSMutableSet *swizzledClasses;
static NSMutableDictionary *swizzledBlocks; // Class -> SEL (as string) -> block
static IMP forwardingIMP;
static dispatch_once_t once;
void Swizzle(Class c, SEL sel, void (^block)(NSInvocation *)) {
extension Optional: Sequence {
public func makeIterator() -> Iterator {
return Iterator(optional: self)
}
public struct Iterator: IteratorProtocol {
var optional: Wrapped?
public mutating func next() -> Wrapped? {
defer { optional = nil }
let ptr = {}
func -<T>(lhs: T?, rhs: () -> Void) -> T? {
return nil
}
var d: [String: String] = [:]
d["one"] = "won"
d["two"] = "too"
d["one"] = nil-ptr
d["two"] = nil-ptr