3

I want to get the information about subviews property of class UIView:

objc_property_t property = class_getProperty([UIView class], "subviews");

But, it returns nil? I think it is so strange. Could someone explain this behavior to me?

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
boyal
  • 353
  • 4
  • 7

2 Answers2

1

Weird. If you use -valueForKey:, it can clearly be shown to exist. This used to be a bug with the old LLVM clang compiler in Xcode 3.2.3, where properties in categories (yes, it is declared in a category on UIView) wouldn't get recognized by the runtime, and there was even a bug report filed here about it. I know recent versions of Xcode have been having trouble with categories of late...

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • I can't reproduce it with my own classes... definitely not that old bug (thought it was, but it's not). I wonder if UIView.h might be a lie. It's possible they didn't actually compile it against that header (seems crazy, but heh...) – Rob Napier Jun 28 '12 at 05:41
  • I can reproduce it on my machine running Xcode DP2. You might be right though, however, I'm inclined to think it's a compiler bug. – CodaFi Jun 28 '12 at 05:54
  • I'm use Xcode 4.3.3. So properties in categories is also be show in the property list of runtime. But subviews property in the document is missing. – boyal Jun 28 '12 at 06:12
  • Add the `-all_load` and `-ObjC` linker flags to your project... I don't know, it's a hunch. – CodaFi Jun 28 '12 at 06:14
  • I have do it. It is same and return nil. My god, the documentation and the header file is a joke? – boyal Jun 28 '12 at 06:24
  • 1
    They must be using some kind of `#include_next` then. I don't know what to say, other than file a bug report. – CodaFi Jun 28 '12 at 06:45
  • 1
    Note that `valueForKey:` and "property" are completely unrelated. The method `subviews` absolutely exists. It just isn't a "property." It'll show up if you `class_copyMethods()`. It's of course possible that there was a compiler bug at the time UIView was compiled. So far I haven't been able to reproduce this effect with my own classes (i.e. without UIView). CodaFi, were you saying you could? I certainly can reproduce it with UIView. – Rob Napier Jun 28 '12 at 12:51
0

I just write a test code:

@interface Cat : NSObject
    @property (nonatomic, strong) NSString *name;
    @property (nonatomic) NSInteger         age;
    @property (nonatomic, readonly, copy)   NSArray *subviews;
@end

It is ok. So Apple maybe be do so magic on it I guess.

boyal
  • 353
  • 4
  • 7
  • Test it with a category. It must be a category. – CodaFi Jun 28 '12 at 06:29
  • I tested using category, like interface Cat (UIViewSimuate) property (nonatomic, strong) NSArray *subviews; end and use NSClassFromString function to get the class, not including the header file. It is the same, the subviews appears in the property list. – boyal Jun 29 '12 at 02:52