Skip to content

Instantly share code, notes, and snippets.

@jjrscott
Created June 18, 2018 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjrscott/26d23f4f2a0fe45701b0876aa3016423 to your computer and use it in GitHub Desktop.
Save jjrscott/26d23f4f2a0fe45701b0876aa3016423 to your computer and use it in GitHub Desktop.
An ObjC equivalent of Swift's `if let`
//
// JJRSIfKind.h
// JJRSFoundation
//
// Copyright © 2018 John Scott ( https://jjrscott.com )
//
// This file is MIT licensed.
// Usage: ifkind (existing variable, required class name) { ... }
#define ifkind(anExistingVariable, aClass) __ifkind(anExistingVariable, aClass, __ifkind_temp(__COUNTER__))
#define __ifkind(anExistingVariable, aClass, tempVariable) \
id tempVariable = [anExistingVariable isKindOfClass:[aClass class]]?anExistingVariable:nil; \
for (aClass * const anExistingVariable = tempVariable,*__ifkind_test = tempVariable; tempVariable && __ifkind_test; __ifkind_test=nil)
#define __ifkind_temp_imp( aCount ) __ifkind_ ## aCount ## __
#define __ifkind_temp( aCount ) __ifkind_temp_imp( aCount )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment