0

One of the method signature in objective-c code

-(void)funcName: (const NSString *)name parameter: (void(^)(ClassName *input)) obj

The class definition header file is

@interface ClassName : NSObject
@property NSObject *data;
@end

Now how to prepare and pass the second paramater?

Dickens A S
  • 3,824
  • 2
  • 22
  • 45

2 Answers2

1

you can pass a block as a parameter for instance like:

option 1.

[... funcName:@"" parameter:^(ClassName * input) {
    NSLog(@"I'm inside the block!");
}];

option 2.

void(^myBlock)(ClassName *) = ^(ClassName * input) {
    NSLog(@"I'm inside the block!");
};

[... funcName:@"" parameter:myBlock];

both options can work, you can use whichever makes more sense to you.

holex
  • 23,961
  • 7
  • 62
  • 76
0

When writing your own methods in Objective-C parameters must be explicitly type casted to the data type that you would like to pass through. The ^ symbol signifys that you are passing in a block. If you would like to pass another parameter after the block - you would simply add the next part of the method name followed by the data type, and then your choice of the variable name. You would do so like this:

-(void)funcName: (const NSString *)name parameter: (void(^)(ClassName *input)) obj withOtherParameter:(NSString *) param {

// Use it here and do what you will
NSLog(@"Param = %@", param);

}

This assumes you are passing in an NSString to be called "param". If you would like to pass in any other data type, just replace the (NSString *) with (NSNumber *) or (NSInteger) before the "param" in the code and then the param variable will be cast as whatever data type you would like.

Brandon A
  • 8,153
  • 3
  • 42
  • 77
  • __1.)__ I am not sure that was asked, __2.)__ your _suggestion_ is a very bad practice because the blocks should be the last parameters conventionally. – holex Aug 04 '15 at 11:51
  • **1.)** The asker of the question used the words, "Now how to prepare and pass the second parameter?". **2.)** He refers to the other parameter as the **second** multiple times. Second infers it would go after the first, therefore, I wrote the code as such. – Brandon A Aug 04 '15 at 12:09
  • You have given the answer **The ^ symbol signifys that you are passing in a block.** but the code is not I wanted, thanks anyway – Dickens A S Aug 04 '15 at 12:14
  • I understand Dickens I apologize I must have misunderstood your question. Glad you understand blocks and passing parameters now! – Brandon A Aug 04 '15 at 12:18
  • the second parameter is about the _block_ which the OP referred to, and the OP has never asked about anything of any third parameter. syntactically your code may be correct but conventionally is wrong, and not a response to the original question. that is all I said. now, you can feel free to correct or remove your answer. – holex Aug 04 '15 at 13:12