respondsToSelector

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

respondsToSelector

Michael Brian Bentley
So I'm converting some code from Objc to C#, and I ask an object in
C# whether it respondsToSelector( "fancyObject:MagicSelector" ). It
says "sure!" and so I try to call it ala object.MagicSelector();

The compiler yells at me because that object does not contain a
definition for MagicSelector and no extension method MagicSelector of
type MonoTouch.Foundation.NSObject could be found.

Do I have to say something like (from my C background):

fancyObject thisUn = (fancyObject)object;
fancyObject.MagicSelector(); ?
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: respondsToSelector

Michael Hutchinson
On Thu, May 19, 2011 at 3:43 PM, Michael Brian Bentley
<[hidden email]> wrote:

> So I'm converting some code from Objc to C#, and I ask an object in
> C# whether it respondsToSelector( "fancyObject:MagicSelector" ). It
> says "sure!" and so I try to call it ala object.MagicSelector();
>
> The compiler yells at me because that object does not contain a
> definition for MagicSelector and no extension method MagicSelector of
> type MonoTouch.Foundation.NSObject could be found.
>
> Do I have to say something like (from my C background):
>
> fancyObject thisUn = (fancyObject)object;
> fancyObject.MagicSelector(); ?

Yes. C# is statically typed, Objective-C messages are basically
dynamically dispatched at runtime. In Obj-C you can dispatch arbitrary
messages to arbitrary objects that they may or may not respond. You
can do something similar in C# with reflection or C# 4 and the
"dynamic" keyword, but let's not go there right now. Static typing is
much more efficient/fast and allows the compiler to catch many kinds
of error at compile-time, but it's also less flexible.

MonoTouch objects that subclass NSObject have counterparts in the
native Objective-C world. The bridge between these instances goes two
ways - wrapper methods on the C# side send messages to the objective-C
object, and exported or overridden methods on the C# side respond to
messages sent to the objective-C object.

Anyway, to cut a long story short, if the method you're calling has
(or could have) an implementation on the objective-C side, you should
send a message by calling a wrapper method (or if one doesn't exist,
use the low-level bridge APIs in MonoTouch.ObjCRuntime.Messaging). If
the method will only ever be implemented in C#, just call the C#
method directly by casting the object to an appropriate subclass or
interface, and instead of using respondsToSelector, check the type of
the object using "is":

if (foo is SomeType) {
    var bar = (SomeType) foo;
    bar.SomeMethod ();
}

Or if you prefer, use the "as" cast, which returns null if the cast
fails, and check for null:

var bar = foo as SomeType;
if (bar != null) {
    bar.SomeMethod ();
}

--
Michael Hutchinson
http://mjhutchinson.com
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: respondsToSelector

Michael Brian Bentley
Huh, excellent. The "as" capability looks like it'll be very useful.
Thanks, appreciate it!

-Mike
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch