Last active
December 11, 2020 17:18
-
-
Save AliSoftware/83df328de4c439808056 to your computer and use it in GitHub Desktop.
ObjCGenerics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
*/ |
Yeah that's a matter of preference but I hear you
FWIW, with the *
as part of the macro, you couldn't do NSArray<id<UITableViewDelegate>>
or whatever.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feels a bit weird having the
*
inside the macro, after I used it a bit.