Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing out parameter

I wrote a method with an out parameter:

-(NSString *)messageDecryption:(NSString *)receivedMessage outParam:(out)messageCondent
{   
    messageCondent = [receivedMessage substringFromIndex:2];
    return [receivedMessage substringToIndex:1];
}

Then I passed the param like this:

NSString *messageCondent;
NSString *mode = [myclassobject messageDecryption:message outParam:messageCondent];

However, there is a problem. The out parameter value is not being set properly. Can any one help me to do this correctly?

like image 256
Vipin Avatar asked May 16 '11 17:05

Vipin


2 Answers

Create the method to accept a pointer to the object.

-(NSString *)messageDecryption:(NSString *)receivedMessage outParam:(NSString**)messageCondent
{   
    *messageCondent = [receivedMessage substringFromIndex:2];
    return [receivedMessage substringToIndex:1];

}

Pass in the reference to the local object.

NSString *messageCondent = nil;
NSString *mode = [myclassobject messageDecryption:message outParam:&messageCondent];
like image 169
tidwall Avatar answered Sep 30 '22 12:09

tidwall


An "out parameter" is by definition a pointer to a pointer.

Your method should look like this:

-(NSString *)messageDecryption:(NSString *)receivedMessage outParam:(NSString **)messageCondent
{   
    *messageCondent = [receivedMessage substringFromIndex:2];
    return [receivedMessage substringToIndex:1];
}

This dereferences the passed-in pointer to get at the actual object reference and then assigns that to whatever [receivedMessage substringFromIndex:2] returns.

Invoking this method is quite simple:

NSString *messageCondent = nil;
NSString *mode = [myclassobject messageDecryption:message outParam:&messageCondent];
like image 40
Jacob Relkin Avatar answered Sep 30 '22 11:09

Jacob Relkin