假设我们有这段代码:
var clientLinesLoaded:Bool = Bool() {
didSet{
print(clientLinesLoaded)
if(clientLinesLoaded == true){
print("->->->->-> clientLinesLoaded")
}else{
print("->->->->-> error loading...")
}
}
}
var clientDatasLoaded:Bool = Bool() {
didSet{
print(clientDatasLoaded)
if(clientDatasLoaded == true){
print("->->->->-> clientLinesLoaded")
}else{
print("->->->->-> error loading...")
}
}
}
当两个变量均设置为“真”
时,我正在尝试触发另一个didSet这就是我的想法...
override func viewDidLoad() {
super.viewDidLoad()
var loaderMustDisappear:Bool = Bool(self.clientLinesLoaded && self.clientDatasLoaded) {
didSet {
print ("value is: ",loaderMustDisappear)
if(loaderMustDisappear){
print("remove loader")
self.cargador.isHidden=true
}else{
print("loading failed, do actions to control")
}
}
}
}
前两个didSet在更改其值时会正确触发。
问题是从来没有调用loaderMustDisappear上的 didSet ,而且我不确定为什么会这样...
有人处理过这样的情况吗?
谢谢!
答案 0 :(得分:0)
我不知道您的代码为什么会正常工作,因为loaderMustDisappear
中声明了viewDidLoad
变量,并且您正在更改clientLinesLoaded
和clientDatasLoaded
的值来自另一个功能。
但是您可以使用closures
来实现。在课程中声明闭包:
var loaderMustDisappearClosure : (()->())?
以及两个Bool
变量:
var clientLinesLoaded:Bool = Bool() {
didSet{
print(clientLinesLoaded)
if(clientLinesLoaded == true){
print("->->->->-> clientLinesLoaded")
self. loaderMustDisappearClosure?()
}else{
print("->->->->-> error loading...")
}
}
}
var clientDatasLoaded:Bool = Bool() {
didSet{
print(clientDatasLoaded)
if(clientDatasLoaded == true){
print("->->->->-> clientLinesLoaded")
self. loaderMustDisappearClosure?()
}else{
print("->->->->-> error loading...")
}
}
}
和viewDidLoad
中的
self.loaderMustDisappearClosure = {
if self.clientLinesLoaded && self.clientDatasLoaded {
print("Disappear Loader")
}
}
self.clientLinesLoaded = true
self.clientDatasLoaded = true
将true
设置为clientDatasLoaded
之后执行此操作的时间。消失的加载器将在控制台中打印。因此,您可以在课堂上的任何地方实现这一点。
希望这对您想要实现的目标有所帮助。
答案 1 :(得分:0)
我不理解您的声明Bool(self.clientLinesLoaded && self.clientDatasLoaded)
,但可能永远不会调用您的变量。尝试在两个变量中都设置loaderMustDisappear
。
我认为下面的代码应该对您有用:
var clientLinesLoaded:Bool = Bool() {
didSet{
print(clientLinesLoaded)
if(clientLinesLoaded == true){
print("->->->->-> clientLinesLoaded")
}else{
print("->->->->-> error loading...")
}
loaderMustDisappear = clientLinesLoaded && clientDatasLoaded
}
}
var clientDatasLoaded:Bool = Bool() {
didSet{
print(clientDatasLoaded)
if(clientDatasLoaded == true){
print("->->->->-> clientLinesLoaded")
}else{
print("->->->->-> error loading...")
}
loaderMustDisappear = clientLinesLoaded && clientDatasLoaded
}
}
var loaderMustDisappear:Bool = Bool() {
didSet {
print ("value is: ",loaderMustDisappear)
if(loaderMustDisappear){
print("remove loader")
cargador.isHidden=true
}else{
print("loading failed, do actions to control")
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
我创建了一个基本代码以便于理解:
class ViewController: NSViewController {
var var1: Bool = Bool() {
didSet{
var12 = var1 && var2
}
}
var var2: Bool = Bool() {
didSet{
var12 = var1 && var2
}
}
var var12: Bool = Bool() {
didSet {
print("var12 is \(var12)")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Testing:
var1 = true
var2 = true
var1 = false
}
}