How to Find Out the Objective-C Generics Type

How can I find out the Objective-C generics type?

The lightweight generics introduced in Xcode 7 are just compile time hints to help the compiler raise warnings, but at run time you get the same old behavior with your variable being just NSArrays of ids.

Source: WWDC '15 "Swift and Objective-C Interoperability" session

See the transcript of the talk:

So the entire lightweight generics feature is based on a type erasure model. Which means that the compiler has all of this rich static type information but it erases that information when generating code.

Generic Type In Objective C

Thats what id is in Objective-C, a untyped Objective-C object pointer.

id test = [[NSMutableArray alloc] init];


How can I create a generic property in Objective-C?

Here is answer.

@interface myParentView< T: parentModel*> :UIView
@property T myObject; // myObject is object of parentModel

In all subclass:

@interface myChildViewOne :myParentView<childModel>
// Now myObject is object of childModel

Obj C has complicated syntax, But we can achieve generic property like above.

lightweight generics in objective c

Objective-C generics really are lightweight, and were added to improve interoperability with Swift, not to make Objective-C code safer. Similar to nullability, think of generics as a way to annotate your interface rather than a reason to change your implementation.

Changing ObjectType to id in the implementation is the best way forward.

Further reading: article on Objective C Generics. Read bob’s comment on that article if you want to know about __covariant.

Using a generic Swift class in Objective-C

Swift generic types cannot be used in Objective-C.

This excludes Swift-only features such as those listed here:

  • Generics
  • ...

In objective-c generics can i pass the type from 1 class to another

Objective-C does not have generics, Apple introduced something they term Lightweight Generics, and you may read the "lightweight" as "faux".

Here is Apple's own statement on what they do:

Allow you to specify type information for collection classes such as NSArray, NSSet, and NSDictionary. The type information improves Swift access when you bridge from Objective-C and simplifies the code you have to write.

Notice the "improves Swift access" and the lack of "improves Objective-C". These lightweight generics are essentially annotations for the Swift compiler when it is importing Objective-C types. On the Objective-C side maintaining the generic invariants is largely the responsibility of the programmer.

You can use the lightweight generic annotations (a) in an @interface and (b) when declaring a variable whose type has lightweight generic annotations. You cannot otherwise use the annotations, in particular you do not use them in an @implementation other than (b) above. Within the @implementation for a lightweight generic annotated @interface rather than use the "type parameters" from the annotations you use the id type - as you have done with your doSomething: method.

The latest Xcode/Clang will provide some checking of lightweight generic annotations in Objective-C, but it is limited and should in no way be taken as a guarantee that the generic conditions are being checked.

In summary, unless you are planning to interwork with Swift using the lightweight generic annotations in Objective-C is of little benefit, and it certainly does not give you "generics" in the usual sense (i.e. parametric generic types).

Undoubtedly not what you wanted to hear, but HTH

Related Topics

Leave a reply