let activateObject:() -> Void = { [weak self] in
DispatchQueue.main.async {
NotificationCenter.default.post(name: .doIt, object: "70733")
self?.spinner.stopAnimation(nil)
self?.ajustObject()
}
}
This closure is inside a function and I would like it to continue inside that function.
I want to define this closure as static. Meaning that the first time the function run, the closure is defined and the next time the function runs, the defined value runs.
I have found solutions like this
func theFunction() {
struct Once {
static let once = Once()
init() {
print("This should be executed only once during the lifetime of the program")
}
}
_ = Once.once
}
but this will not work for this case.
If I do this:
func theFunction() {
struct Once {
static let once = Once()
init() {
let success:() -> Void = { [weak id] in
DispatchQueue.main.async {
NotificationCenter.default.post(name: .doIt, object: "70733")
self?.spinner.stopAnimation(nil)
self?.ajustObject()
}
}
}
_ = Once.once
}
The compiler will complain because I cannot use self inside a struct.
Any way to do this keeping the closure inside the fuction?
thanks, I suspect that your blog post answer my question but I am a newbie on certain matters of swift and I do not have a clue on how to apply your blog answer to my case. Thanks.
If you only want it to apply to one class, then just combine the Person and Object code together in your class and hardcode the type of your class instead of using the general type(of:_) code in Object.
What object is self supposed to refer to? That object needs a spinner property, and an ajustObject method. Since you are not providing a reference to an object, self is assumed to be the instance of the Once struct, which doesn't have those attributes.
Just a minute!!! What calls the activateObject var?
Since all the code seems to be on an instance and not on the class, why not simply add a boolean flag and check for that to avoid calling the same code more than once?
import Foundation
struct Spinner {
func stopAnimation(_ obj: AnyObject?) {
}
}
struct TheStruct {
var spinner : Spinner = Spinner()
func adjustObject() { }
func theFunction() {
struct Once {
static let once = Once()
init() {
let success:() -> Void = { [weak id] in
DispatchQueue.main.async { NotificationCenter.default.post(name: .doIt, object: "70733")
self?.spinner.stopAnimation(nil)
self?.adjustObject()
}
}
}
}
_ = Once.once
}
}
Putting this in a Swift playground, other than the obvious errors on id (not defined) and doIt (not defined), the errors I get from init is that you cannot do a chained un-wrap on a non-optional, i.e., self is not an optional, and that struct Once does not have a property spinner and it does not have a method adjustObject. self is referring to an instance of Once, not the an instance of TheStruct. No mention of not being able to use self in the struct, in fact, self is often used in a class/struct/enum to refer to the local state of the instance.