标签: closures

更新Swift 3的闭包 - @escaping

我已经将我的代码更新为Xcode 8.0 beta 6,但我似乎陷入了关于新的非转义关闭默认值的问题.在下面的代码中,Xcode建议在下面代码的第一行@escaping前面添加completion:,但是仍然不会编译并进入圆圈.*

(编辑:事实上,@scaping应该在之后 添加completion:,正如Xcode建议的那样.警报可能仍然显示,但清理和编译将删除它.)*如何重新编写/修复此代码以在更新的Swift 3中工作?我看过新手册,但找不到合适的代码示例.

func doSomething(withParameter parameter: Int, completion: () -> ()) {
    // Does something

    callSomeOtherFunc(withCompletion: completion)
  }

// Calling the method and execute closure 
doSomething(withParameter: 2) {
  // do things in closure
}
Run Code Online (Sandbox Code Playgroud)

任何帮助非常感谢!

xcode closures escaping swift3

75
推荐指数
2
解决办法
5万
查看次数

在for循环中分配点击处理程序

我有几个div的#mydiv1,#mydiv2,#mydiv3,...,想点击处理分配给他们:

$(document).ready(function(){
  for(var i = 0; i < 20; i++) {
    $('#question' + i).click( function(){
      alert('you clicked ' + i);
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

但是,我没有'you clicked 3'在点击时显示#mydiv3(就像其他每次点击一样)'you clicked 20'.我究竟做错了什么?

javascript jquery closures jquery-selectors

72
推荐指数
4
解决办法
5万
查看次数

在函数中使用关键字 - PHP

可能重复:
在Php 5.3.0中,功能"使用"标识符是什么?一个理智的程序员应该使用它吗?

我一直在研究PHP中的闭包,这引起了我的注意:

public function getTotal($tax)
    {
        $total = 0.00;

        $callback =
            function ($quantity, $product) use ($tax, &$total)
            {
                $pricePerItem = constant(__CLASS__ . "::PRICE_" .
                    strtoupper($product));
                $total += ($pricePerItem * $quantity) * ($tax + 1.0);
            };

        array_walk($this->products, $callback);
        return round($total, 2);
    }
Run Code Online (Sandbox Code Playgroud)

有人请给我一个关于use此代码中使用情况的解释.

function ($quantity, $product) use ($tax, &$total)
Run Code Online (Sandbox Code Playgroud)

当我use在PHP中搜索时,它会找到use在命名空间中使用的关键字,但在这里它看起来不同.

谢谢.

php closures anonymous-function

72
推荐指数
2
解决办法
5万
查看次数

JavaScript中的"闭包"究竟是指什么?

我理解闭包是什么,但我在查找该术语的确切含义时遇到了一些麻烦closure.我已经看过许多网站上使用的术语,但他们很少同意它的实际定义.

  • 是堆栈框架上保留的变量吗?
  • 它是返回的功能吗?
  • 它是外部函数的范围吗?
  • 它是内部(返回)函数的范围吗?
  • 是否可能是在返回函数后将变量保留在堆栈帧上的概念

有人能告诉我到底是什么意思closure吗?

javascript closures definition

71
推荐指数
2
解决办法
2万
查看次数

可以通过闭包访问可变变量.我怎样才能解决这个问题?

我正在通过推特使用Typeahead.我正在接受Intellij的这个警告.这导致每个链接的"window.location.href"成为我的项目列表中的最后一项.

我该如何修复我的代码?

以下是我的代码:

AutoSuggest.prototype.config = function () {
    var me = this;
    var comp, options;
    var gotoUrl = "/{0}/{1}";
    var imgurl = '<img src="/icon/{0}.gif"/>';
    var target;

    for (var i = 0; i < me.targets.length; i++) {
        target = me.targets[i];
        if ($("#" + target.inputId).length != 0) {
            options = {
                source: function (query, process) { // where to get the data
                    process(me.results);
                },

                // set max results to display
                items: 10,

                matcher: function (item) { // how to make sure the …
Run Code Online (Sandbox Code Playgroud)

javascript closures mutable

71
推荐指数
3
解决办法
3万
查看次数

来自Pylint的Cell-var-from-loop警告

对于以下代码:

for sort_key, order in query_data['sort']:
    results.sort(key=lambda k: get_from_dot_path(k, sort_key),
                 reverse=(order == -1))
Run Code Online (Sandbox Code Playgroud)

Pylint报告错误:

循环中定义的单元变量sort_key(cell-var-from-loop)

任何人都可以暗示这里发生了什么吗?从pylint源代码描述是:

闭包中使用的变量在循环中定义.这将导致所有闭包对闭合变量使用相同的值.

但我不知道这意味着什么.谁能举一个这个问题的例子?

python lambda closures

71
推荐指数
2
解决办法
2万
查看次数

PHP 5.4 - '关闭$ this support'

我看到PHP 5.4的新计划功能是:traits,array dereferencing,JsonSerializable接口和称为' closure $this support'的东西

http://en.wikipedia.org/wiki/Php#Release_history

虽然其他人要么立即清楚(JsonSerialiable,阵列解除引用),要么我查找具体(特征),我不确定'封闭$这个支持'是什么.我在谷歌搜索它或在php.net上找到任何关于它的任何东西都没有成功

有谁知道这应该是什么?

如果我不得不猜测,那就意味着:

$a = 10; $b = 'strrrring';
//'old' way, PHP 5.3.x
$myClosure = function($x) use($a,$b)
             {
                 if (strlen($x) <= $a) return $x;
                 else return $b;
             };

//'new' way with closure $this for PHP 5.4
$myNewClosure = function($x) use($a as $lengthCap,$b as $alternative)
                 {
                     if(strlen($x) <=  $this->lengthCap)) return $x;
                     else 
                     {
                         $this->lengthCap++;  //lengthcap is incremented for next time around
                         return $this->alternative;
                     }
                 };
Run Code Online (Sandbox Code Playgroud)

重要性(即使这个例子是微不足道的)是在过去一旦构造了闭包,绑定的"使用"变量是固定的.通过'关闭$ this support',他们更像是你可以搞砸的成员.

这听起来是否正确和/或接近和/或合理?有谁知道这个'封闭$这个支持'是什么意思?

php closures

68
推荐指数
3
解决办法
3万
查看次数

在Groovy闭包中模拟"继续"的最佳模式

看来,Groovy的不支持break,并continue从封闭中.模拟这个的最佳方法是什么?

revs.eachLine { line -> 
    if (line ==~ /-{28}/) {
         // continue to next line...
    }
}
Run Code Online (Sandbox Code Playgroud)

groovy closures

67
推荐指数
4
解决办法
4万
查看次数

关闭与匿名功能(差异?)

可能的重复:
什么是PHP或Javascript中的Closures/Lambda外行术语?
'闭包'和'lambda'有什么区别?

嗨,

我一直无法找到一个明确解释闭包和匿名函数之间差异的定义.

我看到的大多数参考文献清楚地指出它们是不同的"事物",但我似乎无法理解为什么.

有人可以帮我简化一下吗?这两种语言功能之间有哪些具体差异?在哪些情况下哪一个更合适?

closures anonymous-function

67
推荐指数
1
解决办法
2万
查看次数

65
推荐指数
1
解决办法
7万
查看次数