0

I am in the early stages exposed to Objective-c. I have a question is, with one function call like this, the parameters will be passed on like.

time_call = [[NSDate date] timeIntervalSince1970];
[Services get_time:self :@selector(callBackTime:)];

This is the function called

-(void) callBackTime:(NSString *)result 
{
    NSLog(@"result: %@", result);
}

I don't understand result parameter passed to the function type as in the other what was called


I apologize for not stating explicitly question. But here is the original function in Class Service

+(void) get_time:(id)delegate :(SEL)selector
{
    NSLog(@"get time call"); 
}
7
  • Here [Services get_time:self :@selector(callBackTime:)]; you are calling the get_time method. Show the code written inside the get_time method. Commented Jan 15, 2014 at 6:53
  • are you the one who created Services? Commented Jan 15, 2014 at 6:53
  • What is the question? Commented Jan 15, 2014 at 6:54
  • I know that the function callBackTime is called. But I don't understand result parameter passed to the function type as in the other what was called Commented Jan 15, 2014 at 6:55
  • what is Service? className or object ? Commented Jan 15, 2014 at 6:56

4 Answers 4

1

It is unclear what you are not understanding, I suspect English is not your first language - which is OK! Here is a guess in the hope it helps.

Your original get_time:: method in Services does not use its arguments, and as such will never call callBackTime:, so maybe this is why you say you "don't understand result parameter" - without a call no value is obviously passed for this parameter.

Consider this alternate definition of get_time::

+(void) get_time:(id)delegate :(SEL)selector
{
    [delegate performSelector:selector withObject:[NSDate date]]; 
}

Now the method uses its two parameters to invoke the passed method (selector) on the passed object (delegate) - the method requires one parameter and this is supplied ([NSDate date] - the current date & time, picked as your method is called get_time).

If you run this your callBackTime: method should NSLog() the current date.

Hopefully that helps you understand where result comes from - get_time needs to supply it.

HTH

Addendum

I missed that callBackTime: is declared to accept an NSString * and my example passes an NSDate *. The code will work due to Objective-C's lose typing, but to be correct either callBackTime: should take an id or my sample should pass an NSString, e.g. [[NSDate date] description] would do.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry for my question was not clear. My english is not good, but google translate does not seem very efficient. I mean, I do not understand when callBackTime function is called, then the parameters passed to its NSString what type. I do not see it being passed on.
In your original code the callBackTime: method was never called, the selector for the method was passed to get_time:: - passing a selector does not call the method. In my example the method is called and passed a value for its result parameter. callBackTime: "passes on" the value of its parameter to NSLog(), it doesn't pass it on to anything else. Does that help?
1

When you want to send a method to an object. You need two object , one is the object(receiver) the other is selector which describe what the message is. Maybe you need parameters if the selector has.

Here :

time_call = [[NSDate date] timeIntervalSince1970];
[Services get_time:self :@selector(callBackTime:)];

+(void) get_time:(id)delegate :(SEL)selector {
    NSLog(@"get time call"); 
}

delegate is the self(receiver) and selector is @selector(callBackTime:). So in get_time it can send message to delegate like this:

[delegate performSelector:selector withObject:result] ;

Comments

1

If your asking how to pass an object from the class Services maybe you can try something like this:

-(void)callBackTime:(NSString *)result{
    NSLog(@">> %@",result);
}

+(void) get_time:(id)delegate :(SEL)selector {
    if([delegate respondsToSelector:selector]){
        //pass the object here using the selector you have
        [delegate performSelector:selector withObject:@"theObject"];
    }
}

Comments

0

Here you can pass perameter like that

This is what you are doing

 [Services get_time:self :@selector(callBackTime:)];

This is what you have to do for passing parameter

    [Services get_time:self :@selector(callBackTime:yourResultString)];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.