-2

How can I encode on Objective-C the line of javascript:

setTimeout("function("+argument+")", value);
Cœur
  • 37,241
  • 25
  • 195
  • 267
user718016
  • 49
  • 1
  • 5

3 Answers3

1

It is actually not very clear what do you want. I suppose that you want to do that in Objective C way. So you need to use NSTimer:

[NSTimer scheduledTimerWithTimeInterval: value
                                 target: self
                               selector: @selector(function:)
                               userInfo: argument repeats: YES];

-(void) function:(NSTimer*) timer {
   id argument = timer.userInfo;
}

Edit: however if you just want to use that as string then write as Daniel has shown.

Max
  • 16,679
  • 4
  • 44
  • 57
1

Firstly with javascript although you can use a string evaluation in "setTimeout", it's expensive and far less readable.

It is far better to use a reference instead. Please read http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/.

Lets re-write your Javascript as follows:

var myFunction = function (arguments) {
    alert(arguments);
};

var value = 1000;

var myTimeout = setTimeout(myFunction('Hello World'), value);

The above structure translates into the NSTimer class function quite well.

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation *)invocation repeats:(BOOL)repeats

First define your function:

-(void) myFunction {
    // do somthing
}

And to call your function after some delay...

NSTimeInterval value = 1.0;    
NSTimer *myTimeout = [NSTimer scheduledTimerWithTimeInterval:value 
    target:self 
    selector:@selector(myFunction) 
    userInfo:nil 
    repeats:NO];

And there you have it. An Asynchronous function call after a delay of 1 second.

Where are the parameters you may ask? Well it is a little extra step requireing a NSInvocation object which is explained quite nicely at Arguments in @selector

By the way the question "How can I encode on objective-C the line of javascript:" is not clear, i believe you requiring an example similar to the setTimeout method Javascript but in Objective-C

Community
  • 1
  • 1
martin
  • 2,493
  • 1
  • 20
  • 13
-1
@"setTimeout(\"function(\"+argument+\")\", value);"
Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89