Skip to content

Instantly share code, notes, and snippets.

@AliSoftware
Last active December 11, 2020 17:18
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliSoftware/83df328de4c439808056 to your computer and use it in GitHub Desktop.
Save AliSoftware/83df328de4c439808056 to your computer and use it in GitHub Desktop.
ObjCGenerics
// Allow to use generics even if not supported yet
#if __has_feature(objc_generics)
#define NSArrayOf(x) NSArray<x>
#define NSMutableArrayOf(x) NSMutableArray<x>
#define NSDictionaryOf(x,y) NSDictionary<x, y>
#define NSMutableDictionaryOf(x, y) NSMutableDictionary<x, y>
#define NSSetOf(x) NSSet<x>
#define NSMutableSetOf(x) NSMutableSet<x>
#else
#define NSArrayOf(x) NSArray
#define NSMutableArrayOf(x) NSMutableArray
#define NSDictionaryOf(x,y) NSDictionary
#define NSMutableDictionaryOf(x, y) NSMutableDictionary
#define NSSetOf(x) NSSet
#define NSMutableSetOf(x) NSMutableSet
#define __covariant /* allow to use __covariant even if not supported */
#endif
// Allow to use nullability macros and keywords even if not supported yet
#if ! __has_feature(nullability)
#define NS_ASSUME_NONNULL_BEGIN
#define NS_ASSUME_NONNULL_END
#define nullable
#define _Nullable
#define __nullable
#endif
/* Example using them all:
NS_ASSUME_NONNULL_BEGIN
- (NSDictionaryOf(NSString*, NSNumber*)* nullable)dictionaryOfStringLengthsForStrings:(NSArrayOf(__covariant NSString* nullable))*strings;
...
NS_ASSUME_NONNULL_END
*/
@orta
Copy link

orta commented Jul 15, 2015

Feels a bit weird having the * inside the macro, after I used it a bit.

@AliSoftware
Copy link
Author

Yeah that's a matter of preference but I hear you

@robb
Copy link

robb commented Jul 28, 2015

FWIW, with the * as part of the macro, you couldn't do NSArray<id<UITableViewDelegate>> or whatever.

@AliSoftware
Copy link
Author

@orta @robb good point, fixed by removing the * yesterday ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment