How do I get the Objective-C class of an ivar?

前端 未结 3 1834
你的背包
你的背包 2020-12-21 01:04

I have a bunch of simple NSManagedObjects I create in a unit test. They just have a single name attribute of type NSString *. I always

3条回答
  •  囚心锁ツ
    2020-12-21 01:27

    I haven't tried obtaining class information from an ivar, but I know that @property declarations do encode information about the class. For instance, this property declaration:

    @property (copy) NSString *normalString;
    

    results in this attribute string (retrieved using property_getAttributes()) at runtime:

    T@"NSString",C,VnormalString
    

    I've written some open source parsing code for this information.

    Once you have the class name, you can convert it into an actual Class object using NSClassFromString(), and message the result from there.

    Disclaimer: This probably shouldn't be depended upon for production applications, as it is undocumented.

提交回复
热议问题