Objective-Cでプロパティのタイプを動的に決定しようとしています。私がこのサイトや他の場所で読んだことに基づいて、私は正しいことをやっていると信じています。しかし、私のコードは動作していません。Objective-Cプロパティタイプを動的に決定する方法は?
以下のコードスニペットは、この問題を示しています。 「backgroundColorの」とのUIViewの有効な特性でどちらも「フレーム」のプロパティ情報を取得しようと、失敗(class_getProperty()はNULLを返す):プロパティを列挙
id type = [UIView class];
objc_property_t backgroundColorProperty = class_getProperty(type, "backgroundColor");
fprintf(stdout, "backgroundColorProperty = %d\n", (int)backgroundColorProperty); // prints 0
objc_property_t frameProperty = class_getProperty(type, "frame");
fprintf(stdout, "frameProperty = %d\n", (int)frameProperty); // prints 0
をhereが生成しない説明として期待される結果でもあります。以下のコード:
NSLog(@"Properties for %@", type);
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(type, &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
}
この出力を生成する:
2012-03-09 13:18:39.108 IOSTest[2921:f803] Properties for UIView
caretRect T{CGRect={CGPoint=ff}{CGSize=ff}},R,N,G_caretRect
gesturesEnabled Tc,N
deliversTouchesForGesturesToSuperview Tc,N
skipsSubviewEnumeration Tc,N
viewTraversalMark Tc,N
viewDelegate [email protected]"UIViewController",N,G_viewDelegate,S_setViewDelegate:
inAnimatedVCTransition Tc,N,GisInAnimatedVCTransition
monitorsSubtree Tc,N,G_monitorsSubtree,S_setMonitorsSubtree:
backgroundColorSystemColorName [email protected]"NSString",&,N,G_backgroundColorSystemColorName,S_setBackgroundColorSystemColorName:
userInteractionEnabled Tc,N,GisUserInteractionEnabled
tag Ti,N,V_tag
layer [email protected]"CALayer",R,N,V_layer
例えば "backgroundColorの"、 "フレーム" などのプロパティを文書化、および他のものは "caretRect" および "gesturesEnabled" のような文書化されていないプロパティに対し、欠落しています含まれています。
ご協力いただければ幸いです。関連性がある場合は、iOSシミュレータでこの動作が確認されています。実際のデバイスで同じことが起こるかどうかはわかりません。
おかげで、 グレッグ
私はそれについて疑問に思いました。私はUIView.hを見ていきます。ありがとう。 –
うん、それを見る - backgroundColorはUIView(UIViewRendering)というカテゴリで定義されています。カテゴリに定義されているプロパティを取得するにはどうすればよいですか? –
ああ、あなたはそれが可能だとは思わないと言ったことが分かります。それが本当であれば、class_getProperty()がこれらのプロパティを処理するか、category_getProperty()関数を提供する必要があるようです。バグや機能要求を提出する価値はあると思いますか? –