如何确定NSObject是否具有某些属性? [英] how can you find out if an NSObject has a certain property?
本文介绍了如何确定NSObject是否具有某些属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
比方说,在Apple API版本1.0中,存在一个类NSFoo,其属性为颜色". API 1.1添加了属性大小".
Let's say in Apple API version 1.0, there is a class NSFoo with a property 'color'. API 1.1 adds property 'size'.
我想知道是否可以使用getter:myFoo.size
I want to know whether I can use the getter: myFoo.size
[myFoo responsesToSelector:@selector(getSize)]不能按预期工作.
[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.
// ...
}
应该工作.
这篇关于如何确定NSObject是否具有某些属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文