Swift - 使用未解析的标识符'self' - 来自类中的Closure(Swift - Use of unresolved identifier 'self' - from Closure in a Class)
我正在尝试从我的课程中声明的闭包中引用我的课程中的某个属性。 我无法从闭包中访问自己,我假设自己会在我的闭包中引用类API。
我想声明一个闭包,我稍后使用它作为参数传递给URLSession dataTask(它在没有一个错误行的情况下工作)。 我在标题中列出了错误。
使用未解析的标识符'self'
我现在一直在写一整天,而且只是将其作为一个沙箱来试用,所以我完全期待一些批评。
class Api { struct Location { var name = String() var author = String() var averageRating: String? var id = Int() var lat = Double() var lon = Double() var type = String() } var locations = [Location]() var doSomething = {(data: Data?, response: URLResponse?, error: Error?) -> Void in if error != nil { print(error!.localizedDescription) } else { do { if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] { let myResult = json["results"] as! [[String: Any]] var location : Location! = Location() for jsonLocation in myResult { if let name = jsonLocation["name"]{location.name = name as! String} if let author = jsonLocation["author"]{location.author = author as! String} if let id = jsonLocation["id"]{location.id = id as! Int} if let lat = jsonLocation["lat"]{location.lat = lat as! Double} if let lon = jsonLocation["lon"]{location.lon = lon as! Double} if let type = jsonLocation["type"]{location.type = type as! String} //ERROR IS HERE, Why does self not reference class API? self.locations.append(location) } } } catch { print("error in JSONSerialization") } } } }
我发现了这个 ,但这个例子是不同的,所以我不确定它是否是相同的错误,或者我没有迅速理解。
I am trying to reference a property in my class from a closure declared in my class. I cannot access self from inside my closure, and I'm assuming self would refer to the Class API from within my closure.
I want to declare a closure that I use later as a parameter to pass to a URLSession dataTask (It works without the one error line). I get the error listed in the title.
Use of unresolved identifier 'self'
I've been writing swift for an entire day now and am just trying things out as a sandbox, so I fully expect some criticism.
class Api { struct Location { var name = String() var author = String() var averageRating: String? var id = Int() var lat = Double() var lon = Double() var type = String() } var locations = [Location]() var doSomething = {(data: Data?, response: URLResponse?, error: Error?) -> Void in if error != nil { print(error!.localizedDescription) } else { do { if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] { let myResult = json["results"] as! [[String: Any]] var location : Location! = Location() for jsonLocation in myResult { if let name = jsonLocation["name"]{location.name = name as! String} if let author = jsonLocation["author"]{location.author = author as! String} if let id = jsonLocation["id"]{location.id = id as! Int} if let lat = jsonLocation["lat"]{location.lat = lat as! Double} if let lon = jsonLocation["lon"]{location.lon = lon as! Double} if let type = jsonLocation["type"]{location.type = type as! String} //ERROR IS HERE, Why does self not reference class API? self.locations.append(location) } } } catch { print("error in JSONSerialization") } } } }
I have found this, but this example is different so I wasn't sure if it was the same bug or me not understanding swift.
原文:https://stackoverflow.com/questions/43870954
最满意答案
并非每个按钮都可以在单击事件中运行。 此外,还不清楚这是一个静态加载的按钮,还是由AJAX加载。 (链接到目标页面!)
在AJAX驱动的站点上选择并激活正确的控件时会给出一般方法。
像这样的完整脚本可以在99%的情况下工作:
// ==UserScript== // @name _YOUR_SCRIPT_NAME // @include http://YOUR_SERVER.COM/YOUR_PATH/* // @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js // @require https://gist.github.com/raw/2625891/waitForKeyElements.js // @grant GM_addStyle // ==/UserScript== /*- The @grant directive is needed to work around a design change introduced in GM 1.0. It restores the sandbox. */ waitForKeyElements ("#dispatchOnJobButton", triggerMostButtons); function triggerMostButtons (jNode) { triggerMouseEvent (jNode[0], "mouseover"); triggerMouseEvent (jNode[0], "mousedown"); triggerMouseEvent (jNode[0], "mouseup"); triggerMouseEvent (jNode[0], "click"); } function triggerMouseEvent (node, eventType) { var clickEvent = document.createEvent('MouseEvents'); clickEvent.initEvent (eventType, true, true); node.dispatchEvent (clickEvent); }
如果它不适合你,请链接到目标页面,或张贴SSCCE!Not every button works off a click event. Also, it's not clear if this is a statically loaded button, or if it's loaded by AJAX. (Link to the target page!)
A general approach is given in Choosing and activating the right controls on an AJAX-driven site.
Something like this complete script will work in 99% of the cases:
// ==UserScript== // @name _YOUR_SCRIPT_NAME // @include http://YOUR_SERVER.COM/YOUR_PATH/* // @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js // @require https://gist.github.com/raw/2625891/waitForKeyElements.js // @grant GM_addStyle // ==/UserScript== /*- The @grant directive is needed to work around a design change introduced in GM 1.0. It restores the sandbox. */ waitForKeyElements ("#dispatchOnJobButton", triggerMostButtons); function triggerMostButtons (jNode) { triggerMouseEvent (jNode[0], "mouseover"); triggerMouseEvent (jNode[0], "mousedown"); triggerMouseEvent (jNode[0], "mouseup"); triggerMouseEvent (jNode[0], "click"); } function triggerMouseEvent (node, eventType) { var clickEvent = document.createEvent('MouseEvents'); clickEvent.initEvent (eventType, true, true); node.dispatchEvent (clickEvent); }
If it doesn't work for you, Link to the target page, or post an SSCCE!
相关问答
更多-
我想我明白了: 使用window.addEventListener("load", function(){ ... })而不是window.onload = function(){ ... } 。 看来这个window已经有了一个onload函数,它初始化了shuffle按钮的功能,脚本正在重写它。 I think I got it: Use window.addEventListener("load", function(){…}) instead of window.onload = function( ...
-
var oldUrl = window.location.href; var newURL = "http://localhost:8887/processor.php?link=" + oldUrl window.location.replace (newURL); var oldUrl = window.location.href; var newURL = "http://localhost:8887/processor.php?link=" + oldUrl window.loca ...
-
Greasemonkey脚本中的“正常”按钮单击方法不起作用?(“Normal” button-clicking approaches are not working in Greasemonkey script?)[2024-01-26]
并非每个按钮都可以在单击事件中运行。 此外,还不清楚这是一个静态加载的按钮,还是由AJAX加载。 (链接到目标页面!) 在AJAX驱动的站点上选择并激活正确的控件时会给出一般方法。 像这样的完整脚本可以在99%的情况下工作: // ==UserScript== // @name _YOUR_SCRIPT_NAME // @include http://YOUR_SERVER.COM/YOUR_PATH/* // @require http://ajax.googleapis.com/ajax/l ... -
我想知道下面的代码是否是你想要的。 document.querySelector('.friendButton.addFriend a').click() 演示 您最好提供一个指向您希望脚本执行的页面的链接,以便我可以确保脚本按预期工作。 I wonder if the code below is what you want. document.querySelector('.friendButton.addFriend a').click() Demo You'd better provide a l ...
-
发布的代码片段不足,与“按下页面上的按钮关闭窗口”所述的操作不匹配。 因此,最好的猜测是close函数在birthday()之外(其中定义了winNew )或未示出的代码中的某些内容导致问题。 另一个需要注意的是,您需要注意Greasemonkey脚本也可以在子窗口上触发 - 具体取决于打开子窗口时是否指定/指定了哪个URL。 以下是按下按钮打开和关闭子窗口的示例代码: 在jsfiddle看到它的行动 。