14

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 Answer 1

37

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.

3
  • 5
    It should be noted that this is because the default getters for properties omit the get prefix. Unlike setters which have the set prefix.
    – Senseful
    Commented Jun 17, 2010 at 5:25
  • How can I do it the other way around? If I want to set size on Foo? myFoo doesn't know the class properties so I can not access size. Commented Jan 12, 2015 at 18:06
  • 1
    @CristianPena I beleive if you changed "size" to "setSize" in the selector you could then call [myFood setSize:5].
    – csga5000
    Commented Aug 1, 2015 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.