Let's say I define a generic class in Swift, similar to the following:
class MyArray<T> {
func addObject(object: T) {
// do something... hopefully
}
}
(I know there is a slightly better implementation of array, this is merely an example.)
In Swift I can now use this class very easily:
let a = MyArray<String>()
a.addObject("abc")
With Xcode 7, we now have generics in Objective-C, so I would assume that I could use this class in Objective-C:
MyArray<NSString*> *a = [[MyArray<NSString*> alloc] init];
[a addObject:@"abc"];
However, MyArray is never added to my Project-Swift.h
file. Even if I change it to inherit from NSObject, it still doesn't appear in my Swift.h file.
Is there any way to create a generic Swift class and then use it in Objective-C?
Update: If I try to inherit from NSObject and annotate with @objc:
@objc(MyArray)
class MyArray<T>: NSObject {
func addObject(object: T) {
// do something... hopefully
}
}
I get the following compiler error:
Generic subclasses of '@objc' classes cannot have an explicit '@objc' attribute because they are not directly visible from Objective-C.
Does this mean there is no way to use a generic Swift class in Objective-C?
How does one indirectly reference the class?