how can you find out if an NSObject has a certain property?

时光总嘲笑我的痴心妄想 提交于 2020-01-01 08:12:49

问题


Let's say in Apple API version 1.0, there is a class NSFoo with a property 'color'. API 1.1 adds property 'size'.

I want to know whether I can use the getter: myFoo.size

[myFoo respondsToSelector:@selector(getSize)] doesn't work as expected.

What's the correct way to find out if an object has a property? Thanks!


回答1:


You're close. Your selector should be exactly the message you want to send to the object:

if ( [myFoo respondsToSelector:@selector(size)] ) {
    int size = [myFoo size]; // or myFoo.size in dot-notation.
    // ...
}

should work.



来源:https://stackoverflow.com/questions/3059029/how-can-you-find-out-if-an-nsobject-has-a-certain-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!