objective c - Method signature for a Selector -


I'm new to Objective-C business (most of the time Java developer) and now on my first killer app :-) At this time I am confused about the use of selectors as a logic argument in any way. For example, they are looking a bit different in comparison to the representatives in C #.

Looking at the following method signature

  - (zero) executed: (SEL) callback;  

Is there a way to select a method to apply a signature to the selector? The method is expecting a method selector with the following signature

  - (zero) foo: (NSDT *) data;  

But SEL (type) is normal, therefore there is a good chance of passing the wrong selector for the executed method, there will be at least one strange behavior on the runtime ... but if this happens I would like to see a compiler warning / error.

The quick answer is: No, there is no way that the compiler applies the method signature of a method selector Which is provided through the SEL argument.

One of the aims-C's is that it is a vulnerably typed language, which allows for too much dynamic behavior. Of course, this comes at the cost of compilation-time type security.

You want (to think) how to use a representative, the best way is to use "callback" to the other class - Coco uses the delegates to implement methods How can it look:

FooController.h

@protocol FooControllerDelegate @required: - (zero) Handladata: (NSDT *) Data for FU: (FU controller *) Foo; @end @ interface FooController: NSObject {id & lt; FooControllerDelegate & gt; * Representative; } @protect (assign) id & lt; FooControllerDelegate & gt; * Representative; - (zero) doStuff; @end

FooController.m

  @interface FooController (delegate call) - (zero ) Handralata: (NSDTA *) data; @end @implementation FooController @ synthesis representative; - (id) init {if ((self = [super init]) == zero) {return zero; } Delegate = zero; ... self back ...} - (zero) doStuff {... [self-storage data: data]; } - (zero) handle data: (NSDTA *) data {if (delegate! = Zero) {[Representative help data: data foo: self]; } And {return; // or put an error // handle it yourself}} @end  

will send you a representative using the @required keyword in your representative protocol For a FooController that does not implement the method described in the protocol. Attempting to provide a representative who does not match the @required protocol method will result in a compiler error.

Here's how you can create a representative class to work with the above code:

@Interface MyFooHandler & lt; FooControllerDelegate & gt; : NSOgeject {} - (empty) handledata: (NSDTA *) data fu: (FU controller *) Foo; @end @ implementationMyFooHandler- (zero) handledata: (nsdata *) foo for data: (foo controller *) foo {// do something here} @end

and here is the Everything that you will use:

  FooController * foo = [[FooController alloc] init]; Myfoundaler * Phulandler = [[MyFoundand Alok] Init]; ... [foo setDelegate: fooHandler]; // This will be the cause of a compiler error if fooHandler // has not implemented the protocol correctly ... [foo doStuff]; // It will call the delegate method on the woundler ... [Fuhlander release]; [Fu release];  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -