Skip to content

Instantly share code, notes, and snippets.

@bdash
Last active March 13, 2018 22:06
Show Gist options
  • Save bdash/54014e09a79a6145ed26978b5bc23b96 to your computer and use it in GitHub Desktop.
Save bdash/54014e09a79a6145ed26978b5bc23b96 to your computer and use it in GitHub Desktop.
Using Objective-C generics with type constraints on forward-declared classes?
#import <Foundation/Foundation.h>
@interface Base : NSObject
@end
@interface GenericCollection<T: Base *> : NSObject
@end
@class Derived2;
@interface Derived1 : Base
@property (strong) GenericCollection<Derived2 *> *collection;
@end
@interface Derived2 : Base
@property (strong) GenericCollection<Derived1 *> *collection;
@end
@mitzpettel
Copy link

mitzpettel commented May 1, 2016

Deferring the property declaration to a class extension works:

#import <Foundation/Foundation.h>

@interface Base : NSObject
@end

@interface GenericCollection<T: Base *> : NSObject
@end

@interface Derived1 : Base
@end

@interface Derived2 : Base
@property (strong) GenericCollection<Derived1 *> *collection;
@end

@interface Derived1 ()
@property (strong) GenericCollection<Derived2 *> *collection;
@end

In a real-world setting it would likely require introducing an additional header file that both Derived1.h and Derived2.h import.

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