passing out parameter

后端 未结 2 727
深忆病人
深忆病人 2021-01-07 23:35

I wrote a method with an out parameter:

-(NSString *)messageDecryption:(NSString *)receivedMessage outParam:(out)messageCondent
{   
    messageCondent = [re         


        
相关标签:
2条回答
  • 2021-01-07 23:47

    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];
    
    0 讨论(0)
  • 2021-01-08 00:01

    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];
    
    0 讨论(0)
提交回复
热议问题