Is this - or how is this - possible with the new Objective-C generics syntax?
@class MyHelperClass<T>;
@interface NSObject (Extension)
@property (nonatomic,readonly) MyHelperClass<MYTYPE *> * helper;
@end
The question is:
What is the syntax for placeholder MYTYPE *
- and does it exist - that would make this work with any subclass of NSObject. Meaning, that on a UIView
instance, the helper
property would return a MyHelperClass<UIView *>
and on an NSString
instance, it would return MyHelperClass<NSString *>
? __typeof self
does not work as self
is not defined in an interface.
Suggestions are highly appreciated! :)
I don't think this is possible. The most obvious way that I can think of to do this would be the following:
@interface NSObject (Extension)
- (MyHelperClass<instancetype> *)a;
@end
According to the clang documentation,
instancetype
is a contextual keyword that is only permitted in the result type of an Objective-C method
This is a compiler error, however, so perhaps the documentation should read "instancetype is only permitted as the result type of an Objective-C method."
My next thought was to cheat by writing some Swift code
protocol Test {
var a: Array<Self> {get}
}
then importing it into an Objective-C file to see what the preprocessor did to turn it into Objective-C. Sadly, according to the documentation here, generics in Swift can't be accessed from Objective-C -- because they aren't an Objective-C language feature! This document has actually been updated for Swift 2.0, according to the revision history. Probably they still aren't supported because the Objective-C type system isn't as expressive as Swift's even with the currently available generics.
In summary, barring the existence of some obscure clang extension I don't know about, I think there's no possible way to do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With