closures

Swift loadItem closure not running

旧巷老猫 提交于 2021-01-28 09:20:20
问题 I am writing a share extension, but my closure that would capture and save the shared attachment is not running. How can I find out why? The switch branch executes, the attachment is there. There is no error message, it just never runs. if let contents = content.attachments as? [NSItemProvider] { for attachment in contents { let fType = attachment.registeredTypeIdentifiers[0] if attachment.hasItemConformingToTypeIdentifier(fType) { switch fType { case kUTTypeImage as String as String: do {

Why do I have to unwrap a weak self?

只谈情不闲聊 提交于 2021-01-28 03:31:15
问题 I have created a class called VerifyObject , that contains a function with a signature like that typealias handlerCodeID = (String) ->Void class func checkPause(withID:String?, runOnPause: handlerCodeID?) When I run that, I need to pass a weak self reference to inside the closure, using VerifyObject.checkPause(withID: "abcde", runOnPause: {[weak self] (objectID) in self.doSomething() }) Xcode complains that the self in doSomething must be unwrapped to self!.doSomething() why? Does not make

Why do I have to unwrap a weak self?

倾然丶 夕夏残阳落幕 提交于 2021-01-28 02:18:42
问题 I have created a class called VerifyObject , that contains a function with a signature like that typealias handlerCodeID = (String) ->Void class func checkPause(withID:String?, runOnPause: handlerCodeID?) When I run that, I need to pass a weak self reference to inside the closure, using VerifyObject.checkPause(withID: "abcde", runOnPause: {[weak self] (objectID) in self.doSomething() }) Xcode complains that the self in doSomething must be unwrapped to self!.doSomething() why? Does not make

Understanding JavaScript Closures - Freeze variable passed to callback

≯℡__Kan透↙ 提交于 2021-01-28 01:35:29
问题 I do not yet have a basic understanding of JavaScript closures; I have a question regarding a specific situation that perhaps is also basic and common example: Count from 1 to 3 in 3 seconds See JSFiddle here: http://jsfiddle.net/nAh8x/ The code: var i, t; t = 0; // Case A for( i=1; i<=3; i++ ) { setTimeout( function() { log(i); }, t ); t += 1000; } // Case B for( i=1; i<=3; i++ ) { setTimeout( wrapper(i), t ); t += 1000; } function wrapper(i) { return function() { log(i); }; } // Log utility

swift closure cannot override Any

有些话、适合烂在心里 提交于 2021-01-27 13:10:37
问题 Maybe it's a stupid question, but I couldn't find any solutions yet. So, my problem is, that is have an event emitter protocol with a function like this: mutating func on(eventName:String, action:((Any?)->())) { //.. } And I want to use it to inform the listeners whenever an event is triggered with some information. Access token for the "login" event for example. appSessionHadler.on("login") { (weak data: String?) in //... } And than I get an error, that I cannot invoke "on" with that

How To Solve The React Hook Closure Issue?

∥☆過路亽.° 提交于 2021-01-27 12:13:01
问题 import React, { useState} from "react"; import ReactDOM from "react-dom"; function App() { const [count, setCount] = useState(0); function handleAlertClick(){ return (setTimeout(() => { alert("You clicked on: " + count); }, 3000)) } return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}>Click me</button> <button onClick={handleAlertClick}>Show alert</button> </div> ); } I just want to know if this works the way that I think it does, or if there is a better

How To Solve The React Hook Closure Issue?

匆匆过客 提交于 2021-01-27 12:07:29
问题 import React, { useState} from "react"; import ReactDOM from "react-dom"; function App() { const [count, setCount] = useState(0); function handleAlertClick(){ return (setTimeout(() => { alert("You clicked on: " + count); }, 3000)) } return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}>Click me</button> <button onClick={handleAlertClick}>Show alert</button> </div> ); } I just want to know if this works the way that I think it does, or if there is a better

How do you know what parameters/arguments to put in a closure?

余生长醉 提交于 2021-01-27 09:03:51
问题 When do closures have parameters (or how do closures with parameters work)? I know that use() is used to import variables outside the anonymous function, but what about the parameter(s) of the closure itself? 回答1: An example of closures with parameters is currying: function greeter($greeting) { return function($whom) use ($greeting) { // greeting is the closed over variable return "$greeting $whom"; }; } $hello_greeter = greeter('hello'); echo $hello_greeter('world'); // will print 'hello

How do you know what parameters/arguments to put in a closure?

半腔热情 提交于 2021-01-27 09:01:36
问题 When do closures have parameters (or how do closures with parameters work)? I know that use() is used to import variables outside the anonymous function, but what about the parameter(s) of the closure itself? 回答1: An example of closures with parameters is currying: function greeter($greeting) { return function($whom) use ($greeting) { // greeting is the closed over variable return "$greeting $whom"; }; } $hello_greeter = greeter('hello'); echo $hello_greeter('world'); // will print 'hello

Why this JavaScript property returns empty string, while JavaScript function works fine?

梦想的初衷 提交于 2021-01-27 07:22:29
问题 Consider this simple JavaScript module pattern: var human = (function () { var _name = ''; return { name: _name, setName: function (name) { _name = name; } } })(); human.setName('somebody'); alert(human.name); // shows an empty string human = (function () { var _name = ''; return { name: function() { return _name; }, setName: function (name) { _name = name; } } })(); human.setName('somebody'); alert(human.name()); // shows 'somebody' Why the second closure works fine, while the first closure