Objective-C - Reflection - NSArray Generic Type

Hello,

I'm trying to access the generic type of my NSArray property via reflection API.
Here's my defined property:
Code Block objective-c
@property(nonatomic) NSArray<CustomObject*>* tabs;


And here is my code how I access my the property via reflection:

Code Block objective-c
objc_property_t *properties = class_copyPropertyList(class, &propertyCount);
for (unsigned int i = 0; i < propertyCount; i++) {
objc_property_t property = properties[i];
objc_property_t property = properties[i];
const char *propertyName = property_getName(property);
const char *attrs = property_getAttributes(property);
}


propertyName = "tabs" is correct
attrs = "T@"NSArray",&,N,V_tabs"

It seems like there is no information about the generic type.
Also in the documentation I can't find any information about this case.
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW5

Isn't it possible to access the generic type of my NSArray property via reflection?

It seems like there is no information about the generic type.

That’s correct. Objective-C’s lightweight generics are a compile-time only feature; they have no visibility in the runtime.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
I've found a working solution.

If I add a protocol to my definition
Code Block objective-c
@protocol CustomObject
@end


I can declare my property like
Code Block objective-c
@property(nonatomic) NSArray<CustomObject*><CustomObject>* tabs;


And this way I get the information about my generic type. The output will be:


T@"NSArray<CustomObject>",&,N,V_tabs

But I don't understand the syntax of my NSArray definition. Can someone explain it to me?

But I don't understand the syntax of my NSArray definition.

Consider this, from <Foundation/NSStream.h>:

Code Block
@property (nullable, assign) id <NSStreamDelegate> delegate;


It’s says “delegate is a property that must be an object (id) that conforms to the NSStreamDelegate protocol.” So, by extension, the <CustomObject> part of your declaration says “tabs is a property that must be an NSArray that conforms to the CustomObject protocol. In addition, the <CustomObject*> part of your declaration is as before, a lightweight generics decorator that says “The elements of the tabs array will be objects of type CustomObject.”

This is, I’m sorry to say, nonsense.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Objective-C - Reflection - NSArray Generic Type
 
 
Q