简体   繁体   English

Swift:具有全局变量的属性观察器

[英]Swift: Property observer with a global variable

I have two files , and in one of them, a variable is set ' on button click '. 我有两个文件 ,在其中一个文件中,将变量设置为“ 单击按钮时 ”。

class Home: UIViewController {

    var swipeNumber = 99

    @IBAction func pressedThreeSwipes(sender: AnyObject) {
        swipeNumber = 3
    }
}

Then in a second file ( which acts as a second view ), I have a didSet property observer that is checking for a change in the variable swipeNumber . 然后在第二个文件( 用作第二个视图 )中,我有一个didSet属性观察器 ,它正在检查变量swipeNumber的更改

func didMoveToView(view: UIView) {
    /* Setup your scene here */

    var menu = Home()

    var swipeNumber: Int = 0 {
        didSet {
            println("\(menu.swipeNumber) should be three.")
        }
    }
}

However, when you run the program , the println line does not operate , suggesting that the variable swipeNumber hasn't been changed . 但是, 当您运行该程序时println行不会运行这表明变量swipeNumber尚未更改

So I thought that this would be a problem with the = 0 in the property observer, but if I remove that, I receive an error about having no initialisers. 因此,我认为这将是属性观察器中= 0的问题,但是如果删除它,则会收到有关没有初始化程序的错误。

However, after double checking , I realised what (I think) the problem is. 但是, 经过仔细检查 ,我意识到了问题所在。 As the didSet code doesn't actually 'do anything', I knew that it was not recognising a change in the variable . 由于didSet代码实际上并未“执行任何操作”,因此我知道它无法识别变量中的更改 I think this is because the button is clicked and the variable 'changed' before the view loads , ie the variable has to be changed with the second view is open . 我认为这是因为在视图加载之前单击了按钮并且变量“已更改” ,即必须在打开第二个视图的情况下更改变量 I think the problem is where I am putting the property observer . 我认为问题出在我要安置财产观察员的地方 Perhaps I should put it in AppDelegate or somewhere else, and link that to my second view? 也许我应该将其放在AppDelegate或其他地方,并将其链接到第二个视图?

Any advice you could give me would be greatly appreciated. 您能给我的任何建议将不胜感激。

Thank you in advance. 先感谢您。

EDIT 1: Following @utahwithak's advice, I put a property observer inside the AppDelegate file: 编辑1:按照@utahwithak的建议,我在AppDelegate文件中放置了一个属性观察器:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var swipeNumber: Int = 0 {
        didSet {
            println("\(swipeNumber)")
        }
    }
}

I also changed menu to be appDelegate . 我也menu更改为appDelegate Now I get the message '3' when I press pressedThreeSwipes . 现在,当我按下pressedThreeSwipes时,我收到消息“ 3”。 However, when I go to the next view and play, a different message tells me that swipeNumber = 0. I think this is to do with the first line of the property observer in my AppDelegate file . 但是,当我转到下一个视图并播放时,另一条消息告诉我swipeNumber =0。我认为这与AppDelegate文件中属性观察器第一行有关 Thank you. 谢谢。

In the first file it is setting the local member variable that has no didSet This is a different swipeNumber than in your second file and setting that will not fire the didSet in the second file. 在第一个文件中,它设置了没有didSet的本地成员变量,这与第二个文件中的swipeNumber不同,并且设置为不会触发第二个文件中的didSet。

In file 2 is it a copy paste error or are you declaring menu and swipeNumber inside your function? 在文件2中是复制粘贴错误,还是在函数中声明了menuswipeNumber If they are in the function that is very likely the wrong spot, they should be at the same level of your function for a class member or outside the class declaration for a global variable. 如果它们在函数中很可能是错误的位置,则对于类成员,它们应该与函数处于同一级别;对于全局变量,它们应位于类声明的外部。

I'm not sure I understand what you're trying to accomplish but if you want file 2 to be aware of file 1 information there are many ways without using a global variable. 我不确定我了解您要完成的工作,但是如果您希望文件2知道文件1的信息,则有许多方法可以不使用全局变量。 Such as a delegate that file 1 calls on a didSet or posting an NSNotification through NSNotificationCenter 例如文件1调用didSet或通过NSNotificationCenter发布NSNotification的委托

Edit for Delegate example 编辑代表示例

in file 1 you could have something like this outside your class declaration 在文件1中,您可以在类声明之外添加类似的内容

protocol SwipeNumberDelegate{
    func swipeNumberChanged(newVal:Int)
}

then inside your file 1 class would have something like this 那么在您的文件1类内部将有这样的内容

class Home: UIViewController {
    var swipeDelegate:SwipeNumberDelegate? = nil
    var swipeNumber = 99{
         didSet{
            if let delegate = self.swipeDelegate{
                delegate.swipeNumberChanged(self.swipeNumber)
            }
         }
    }
 ...
}

somehow from file 1 or file 2 you have to then set swipeDelegate 从文件1或文件2以某种方式必须设置swipeDelegate

and in File 2 in your class declaration you'll have to say it conforms to the protocol by making it look something like: 在类声明的文件2中,您必须通过使其看起来像这样来使其符合协议:

class mySecondClass:SuperClass, SwipeNumberDelegate

and implement the protocol 并实施协议

func swipeNumberchanged(newVal:Int){
    println("Number Changed:\(newVal)")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM