Reputation: 11686
I'm a JS developer learning Obj-C. I have most of the basics down but I'm getting hung up on some concepts. It would be extremely helpful for me to see an objective-c translation of the following functions.
var doSomething = function (callback) {
//some work
}
var start = function (interval, other, cb) {
setInterval(doSomething(function(result){
total = result + other
cb(total)
}), interval)
}
start(1000, 10, function(total){
console.log(total)
})
Thanks very much for any help.
Upvotes: 0
Views: 156
Reputation: 23558
this is not a full translation as there are some pieces of your code that seem to be missing (see my comment above).. but this will at least give you a starting point:
var doSomething = function (callback) {
//some work
}
in obj-c this is more or less the same as a block so you can write something like this:
void (^doSomething)(id) = ^(callback) {
// some work
}
I won't explain the syntax, that's what the link above is for. Since javascript is loosely typed, and you haven't specified what datatype callback is.. i set the callback parameter as a generic id
in obj-c, which can stand for any class (including a block)
var start = function (interval, other, cb) {
setInterval(doSomething(function(result){
total = result + other
cb(total)
}), interval)
}
here you are basically calling doSomething
and applying an anynomous function on the result callback, and you are making this call in every interval
milliseconds.
In objective-c, you use the NSTimer to do what setInterval does.
The NStimer calls a selector (ie method).. so we just create a method that wraps around our block:
-(void)blockWrapper:(id)argument {
doSomething(argument);
}
then we create the NSTimer with that method (see reference of below method here):
// here interval is in seconds, in your code it is in milliseconds
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@SEL(blockWrapper:)
userInfo:nil
repeats:YES];
update:
so now that I know what start is, we basically define it as such (note: here I use a modification made to the original + scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: above.. I borrowed the modification from here.. basically instead of having the NSInterval run on a selector, the modification makes it run on a block. Notice the modification code assumes the block has no arguments.. I would change that code and make it expect an argument of type void(^blockName)((void)(^otherBlockname)(void))
..
- (void)start:(int)interval other:(int)other callback:(void)((^)(int total))callback {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:interval
repeats:YES
actions:doSomething(^(result) {
int total = result + other;
callback(total);
}
}
then finally instead of
start(1000, 10, function(total){
console.log(total)
})
you'd have:
[start:1000 other:10 callback:^(total) {
NSLog(@"%d",total);
}];
final notes: the above code is pretty advanced. I strongly recommend you take each part and practice it on its own (ie look at the block article I referenced above.. take your time reading it.. it takes a while for the block syntax to sink in.. it's not as straight forward as anonymous functions in javascript b/c you have to be very specific with the arguments. Again, javascript is a loosely typed language, obj-c isn't).
Upvotes: 2
Reputation: 883
First of all, the code you provided in somehow incomplete. The variable "result" is never defined in the scope of your snippet.
There are a few things to remember when passing from a script language like javascript to a modern object oriented language like objective-c:
All methods are preferred to be defined prior calling. Anonymous methods are rarely used but it still can be done via blocks.
Variables needs to be strongly typed most of the time unlike javascript.
Variables do not hold methods (except for blocks witch require a bit of variable weak/string references understanding)
Variables and methods do not exist unless you declare them. You cannot do total = result + other because total do not exist yet. You have to declare it first doing CGFloat total = result + other.
A similar code like the one you provided could look like this in objective C:
- (CGFloat)cb:(CGFloat)input
{
return input * <Whatever calculation you perform>;
}
- (CGFloat)preDoSomething:(CGFloat)result
{
//Some work
CGFloat total = result + other;
total = [self cg:total];
return total;
}
- (CGFloat)doSomethingWithResult:(CGFloat)result andInterval:(NSRange)interval
{
//Some work
return result;
}
- (void)startWithInterval:(NSRange)interval other:(CGFloat)other cb:(CGFloat)cb
{
[self setInterval:[self doSomethingWithResult:[self preDoSomething:result] andInterval:interval]];
}
UPDATE
I see what you are trying to do now with setInterval call. In that case, stick with the prior answer using NSTimer.
Upvotes: 1