closures

Swift programmatically create function for button with a closure

余生长醉 提交于 2019-12-01 16:44:24
In Swift you can create a function for a button like this: button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside) However is there a way I can do something like this: button.whenButtonIsClicked({Insert code here}) That way I do not even have too declare an explicit function for the button. I know I can use button tags but I would prefer to do this instead. Create your own UIButton subclass to do this: class MyButton: UIButton { var action: (() -> Void)? func whenButtonIsClicked(action: @escaping () -> Void) { self.action = action self.addTarget(self, action:

What does “Access to disposed closure” mean?

独自空忆成欢 提交于 2019-12-01 16:38:30
问题 I have the following code: public void DequeueRecipe(AuthIdentity identity, params Guid[] recipeIds) { using (var session = GetSession()) { var recipes = (from r in recipeIds select new Models.Recipes {RecipeId = r}).ToArray(); var dbRecipes = session.QueryOver<Models.QueuedRecipes>() .Where(Expression.Eq("UserId", identity.UserId)) .Where(Expression.InG("Recipe", recipes)) .List<Models.QueuedRecipes>(); using (ITransaction transaction = session.BeginTransaction()) { dbRecipes.ForEach(r =>

Debugging Revealing Module Pattern: functions not in scope until called?

自闭症网瘾萝莉.ら 提交于 2019-12-01 15:53:26
问题 If I run this code within the Chrome Developer Tools: var test = (function () { var publicFunction, privateFunction1, privateFunction2; privateFunction1 = function privateFunction1() { return true; }; privateFunction2 = function privateFunction2() { return true; }; publicFunction = function publicFunction() { privateFunction1(); debugger; }; return { publicFunction: publicFunction }; })(); why is privateFunction1 in scope at the breakpoint, while privateFunction2 is not? 回答1: Fascinating

How to removeEventListener that was added using closure?

本小妞迷上赌 提交于 2019-12-01 15:52:46
This is basically a followup question to this: Can't pass event to addEventListener: closure issue . I have read almost every related question and can't find the answer to this. The function below is executed within a loop where the parameters are pulled from an array of data. With this function I am able to pass different/new parameters to each instance of an event listener. The outerfunction allows for the values of the parameters to be encapsulated within closure so that the actual values are available, not just references to the holders. Additionally, the passeventfunction passes the event

Referring to “this” in a parent closure in javascript

北城以北 提交于 2019-12-01 15:48:22
I want to do this in Javascript: function Z( f ) { f(); } function A() { this.b = function() { Z( function () { this.c() } ); } this.c = function() { alert('hello world!'); } } var foo = new A(); foo.b(); It can be accomplished this way: function Z( f ) { f(); } function A() { var self = this; this.b = function() { Z( function () { self.c() } ); } this.c = function() { alert('hello world!'); } } var foo = new A(); foo.b(); Is there a better way? Keeping a reference to the parent (like you have) is a good approach, however for your specific example there's no need for the anonymous wrapper, you

How to understand closures in Javascript? [duplicate]

ぐ巨炮叔叔 提交于 2019-12-01 15:47:27
This question already has an answer here: How do JavaScript closures work? 86 answers How can one understand closures in Javascript? In general terms, a closure is a function bound to one or more external variables. When it is called, the function is able to access these variables. In JavaScript, closures are often implemented when functions are declared inside another function. The inner function accesses variables of the parent one, even after the parent function has terminated In this statement, "a closure is a function bound to one or more external variables", does it mean we can do this :

Swift programmatically create function for button with a closure

拥有回忆 提交于 2019-12-01 15:41:51
问题 In Swift you can create a function for a button like this: button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside) However is there a way I can do something like this: button.whenButtonIsClicked({Insert code here}) That way I do not even have too declare an explicit function for the button. I know I can use button tags but I would prefer to do this instead. 回答1: Create your own UIButton subclass to do this: class MyButton: UIButton { var action: (() -> Void)?

Referring to “this” in a parent closure in javascript

倖福魔咒の 提交于 2019-12-01 15:35:29
问题 I want to do this in Javascript: function Z( f ) { f(); } function A() { this.b = function() { Z( function () { this.c() } ); } this.c = function() { alert('hello world!'); } } var foo = new A(); foo.b(); It can be accomplished this way: function Z( f ) { f(); } function A() { var self = this; this.b = function() { Z( function () { self.c() } ); } this.c = function() { alert('hello world!'); } } var foo = new A(); foo.b(); Is there a better way? 回答1: Keeping a reference to the parent (like

Why does resharper suggest “wrap variable in array” for access to modified closure warnings?

泪湿孤枕 提交于 2019-12-01 14:57:55
Given the following (heavily edited, pseudo-)code: int count = 0; thing.Stub(m => m.AddBlah()).WhenCalled(o => count++); thing.Stub(m => m.RemoveBlah()).WhenCalled(o => count--); DoStuff(thing); Assert.AreEqual(1, count); ReSharper provides a warning on count - "Access to modified closure". I understand why I'm getting this warning (the count variable is being modified in two different lambdas, and is likely to have undesirable semantics), but I don't understand ReSharper's advice: "Wrap local variable in array". If I let ReSharper do this, I get: int count[] = { 0 }; thing.Stub(m => m.AddBlah

How to add @noescape annotation to optional closure

两盒软妹~` 提交于 2019-12-01 14:33:05
问题 My function has this signature: func foo(bar: String, baz: ((String) -> ())? = nil) And now I want to make unecessary to escape self inside the given closure. But when I try this: func foo(bar: String, @noescape baz: ((String) -> ())? = nil) The compiler complains: @noescape may only be applied to parameters of function type Is it possible to use it in optional parameters? 回答1: Requirements If your requirements are the following: the baz param is a closure the baz param is marked with