开发者

objc_property_t iOS issue

If I understood from docs with this :

objc_property_t theProperty = class_getProperty([self class], [name UTF8String]);
开发者_高级运维

I get the referance to the property. I don't understand how to "cast" this struct to UIImageView *imageView_1 for example, to be able to set image to this imageVIew_1.


objc_property_t is an opaque type. It's basically a C struct describing the property, at the class level. It has nothing to do with a value for a specific instance.

So in other words, you can't get a property value using class_getProperty.

Also remember that a property implies a getter method. So if you want to access a property, you can call the getter method.

Note that you can get the value for an instance variable by using the object_getInstanceVariable function.

Ivar object_getInstanceVariable(id obj, const char *name, void **outValue)

By the way, why are you using the runtime to access a property?

Maybe you are doing some very complicated reflection stuff, but otherwise, I'll suggest to avoid the runtime.

What's wrong with:

object.propName

or

[ object propName ];

?


Hm, how to put this but there is much easier way to solve this problem and its name is KVC. Something like this :

UIImageView *img = [self valueForKey:[NSString stringWithFormat:@"image%d", floor(myFloat)]];

Obviously because of lack of knowledge I choose a harder way with runtime stuff. I used KVC in conjunction with Core Data and didn't realize all the possibilities. Well learning never ends :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜