开发者

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

Let's say in Apple API versio开发者_JAVA百科n 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!


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜