Objective-C方法/函数调用。 (来自javascript示例)
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.
我是一名学习Obj-C的JS开发人员。我有大部分的基础知识,但我已经挂了一些概念。看到以下功能的客观c翻译对我来说非常有帮助。
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.
非常感谢您的帮助。
2 个解决方案
#1
2
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:
在obj-c中,这或多或少与块相同,因此您可以编写如下内容:
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)
我不会解释语法,这就是上面的链接。由于javascript是松散类型的,并且你没有指定回调是什么数据类型..我将回调参数设置为obj-c中的通用id,它可以代表任何类(包括块)
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.
在这里,您基本上调用doSomething并在结果回调上应用任意函数,并且您在每个间隔毫秒内进行此调用。
In objective-c, you use the NSTimer to do what setInterval does.
在objective-c中,您使用NSTimer来执行setInterval所做的事情。
The NStimer calls a selector (ie method).. so we just create a method that wraps around our block:
NStimer调用一个选择器(即方法)..所以我们只创建一个包裹我们块的方法:
-(void)blockWrapper:(id)argument {
doSomething(argument);
}
then we create the NSTimer with that method (see reference of below method here):
然后我们用该方法创建NSTimer(参见下面方法的参考):
// 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))
..
所以现在我知道什么是开始,我们基本上定义它(注意:这里我使用对原始+ scheduledTimerWithTimeInterval进行的修改:target:selector:userInfo:重复:上面..我从这里借用了修改..基本上而不是在选择器上运行NSInterval,修改使它在一个块上运行。注意修改代码假定块没有参数。我会更改该代码并使其期望类型为void的参数(^ blockName)( (无效)(^ otherBlockname)(无效))..
- (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).
最后的注释:上面的代码非常先进。我强烈建议你把每个部分都单独练习(比如看看我上面引用的块文章。花点时间阅读它...块语法需要一段时间才能沉入..它不是那么简单作为javascript b / c中的匿名函数,你必须对参数非常具体。再次,javascript是一种松散类型的语言,obj-c不是)。
更多相关文章
- javascript 的MD5代码备份,跟java互通
- 我可以在JavaScript中从不同的页面传递参数吗?
- 这些年,我收集的JavaScript代码(二)
- 确保代码在*之后执行*对监视属性的更改已在UI中生效
- JavaScript:使用函数参数检索javascript对象键
- 如何用NodeJS组织构建、服务器、客户端和共享JavaScript代码
- 如何在Node中创建可重用的函数而不编写样板代码
- Js中获取超链接里面传递的参数值
- 如何使用AngularJS获取url参数