Skip to content

Instantly share code, notes, and snippets.

//
// CDZIdioms.h
// https://www.dzombak.com/blog/2015/02/Tiny-Swift-idioms-in-ObjC.html
//
// Created by Chris Dzombak on 3/21/15.
// Copyright (c) 2015 Chris Dzombak. All rights reserved.
//
#ifndef CDZIdioms_h
#define CDZIdioms_h
@cdzombak
cdzombak / lazy_get.m
Created February 27, 2015 04:44
simple ObjC lazy getter macro
#import <Foundation/Foundation.h>
#define lazy_get(TYPE, NAME, VALUE) \
@synthesize NAME = _##NAME; \
- (TYPE)NAME { if (!_##NAME) _##NAME = (VALUE); return _##NAME; }
// example usage follows:
@interface LazyThing : NSObject
@cdzombak
cdzombak / as.m
Created February 26, 2015 18:55
swift-style as in ObjC
#import <Foundation/Foundation.h>
#define as_checked(EXPR, KLASS) ({ id _obj = EXPR; NSCAssert([_obj isKindOfClass:[KLASS class]], @"Cannot cast %@ to %@", NSStringFromClass([_obj class]), NSStringFromClass([KLASS class])); _obj; })
#define as_option(EXPR, KLASS) ({ id _obj = EXPR; if (![_obj isKindOfClass:[KLASS class]]) _obj = nil; _obj; })
@interface NSObject(As)
- (instancetype)asChecked:(Class)klass;
@cdzombak
cdzombak / iflet.m
Last active January 7, 2016 16:05
if-let in ObjC
#import <Foundation/Foundation.h>
#define iflet(LHS, RHS) \
for (id obj_ = (RHS); obj_ != nil;) \
for (LHS = (obj_ ?: (RHS)); obj_ != nil; obj_ = nil)
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString *x = nil;
NSString *y = @"y";
@cdzombak
cdzombak / gist:16351244498cdb2f383c
Created February 1, 2015 21:33
Effective Organization for Developers, talk abstract

You have a lot of responsibilities and plenty of to-dos to keep track of. Add in your email and Instapaper and it can feel impossible to make any progress. But it's not! In this talk we’ll discuss basic principles you can use to organize yourself, stay atop your responsibilities, and destress at the same time. We’ll also cover some common organizational pitfalls that we as programmers can fall into, and we’ll briefly discuss tooling, including the pros and cons of using a tool like OmniFocus.

public class Router : NSObject {
typealias VCPushBlock = (UIViewController) -> (Void)
init(context: NSManagedObjectContext, pushBlock: VCPushBlock) {
// ...
}
convenience init(context: NSManagedObjectContext, owningViewController: UIViewController) {
self.init(context: context, pushBlock: {
owningViewController.navigationController?.pushViewController($0, animated: true)
@cdzombak
cdzombak / Router.swift
Created January 22, 2015 00:53
Swift compilation issue that took some of my time today. Solution: https://gist.github.com/cdzombak/431cea0a4c0ced8042a5
public class Router : NSObject {
typealias VCPushBlock = (UIViewController) -> (Void)
init(context: NSManagedObjectContext, pushBlock: VCPushBlock) {
// ...
}
convenience init(context: NSManagedObjectContext, owningViewController: UIViewController) {
self.init(context: context, pushBlock: {
owningViewController.navigationController?.pushViewController($0, animated: true)
@cdzombak
cdzombak / a.md
Created January 19, 2015 00:48
problems with "due process compatible" cryptosystems
  • Barring serious fundamental crypto breakthroughs, it's not technically possible to introduce a back door for law enforcement without putting the users at risk. Any back door (or "front door," which isn't a real thing in cryptosystem design) represents an additional attack vector. For example, a hypothetical key store for an encrypted messaging service, even if designed to be used only after due process is applied, represents a huge risk to all users of the system, from other nation-states or other organizations.

  • More importantly in real-world scenarios, these encrypted messages will be around forever, and "due process" is malleable. In early-2000s America, one can imagine a suspension of due process in the investigation of a major terrorist attack or other act of war; in other countries, due process is redefined or taken away after a regime change. Then every message sent by anyone who has ever used the system is vulnerable.

@cdzombak
cdzombak / self.swift
Created December 17, 2014 18:55
Is omitting `self`, per [github/swift-style-guide](https://github.com/github/swift-style-guide#only-explicitly-refer-to-self-when-required) always the best? In this example, I argue: yes, the second version is more verbose, but that verbosity is a good thing; it adds clarity to the intention of this method.
override func loadView() {
let webView = UIWebView()
webView.delegate = self
webView.backgroundColor = UIColor.nl_offWhite()
view = webView
}
// vs.