如何将变量从类传递到另一个?
目标
我需要我的应用传递变量的值 myVariable
仅当变量更改其值时,才从类 firstClass
到另一个 secondClass
。
I need my app to pass the value of a variable myVariable
from a class firstClass
to another secondClass
only when the variable changed its value.
代码
为此,我想到了使用 willSet
属性。虽然,在Swift中你不能在声明变量之后使用它。
To do so, I thought of using the willSet
property. Though, in Swift you can't use it after the declaration of the variable.
class firstClass: NSObject {
var myVariable = 0
func myFunction {
myVariable = 5
}
}
class secondClass: NSObject {
var otherClass = firstClass()
// How do I retrive the value of the variable right after its value changed?
}
我还想过添加 NSNotification
,但这没有用,因为它没有传递值。 NSNotification
仅提醒其更改。
I also thought of adding a NSNotification
, but that wouldn't help because it doesn't pass a value. NSNotification
only alerts about its changes.
let myVariableNotification = NSNotification(name: "myVariableNotification", object: nil)
class firstClass: NSObject {
var myVariable = 0
func myFunction {
myVariable = 5
notificationCenter.postNotification(myVariableNotification)
}
}
class secondClass: NSObject {
var otherClass = firstClass()
NSNotificationCenter.defaultCenter().addObserverForName("myVariableNotification",
object: nil,
queue: NSOperationQueue.mainQueue()
usingBlock: { notification in
println("The variable has been updated!")
})
}
问题
一旦变量改变了值,我似乎无法传递变量。我该怎么做?
I seem to find no way to pass a variable once that variable changed its value. How can I do that?
您应该使用委托协议。有关详细信息,请查看此文档。
You should use delegate protocols. For more information check out this document.
在 secondClass
中设置协议
,对在 import
语句之后,如下所示:
Set up a protocol
in the secondClass
, right after the import
statements, like so:
protocol InformingDelegate {
func valueChanged() -> CGFloat
}
在同一个 secondClass $ c $内c>创建一个
委托
变量(有人建议它应该标记为 weak
):
Inside the same secondClass
create a delegate
variable (some suggest that it should be marked as weak
):
var delegate: InformingDelegate?
然后,创建一些方法,您将在其中访问更改的值。您可以将其分配给值
,例如:
Then, create some method in which you will access the changed value. You can assign it to value
for example:
func callFromOtherClass() {
value = self.delegate?.valueChanged()
}
这适用于 secondClass
。现在进入 firstClass
。
这里你只需要通过添加 InformingDelegate
来符合协议在类定义之后,如下所示:
This is it for the secondClass
. Now onto the firstClass
.
Here you only need to conform to the protocol by adding InformingDelegate
after the class definition, like this:
class firstClass: UIViewController, InformingDelegate {
...
}
然后,通过创建它来通知编译器您将成为另一个类的委托实例,并将自己设置为委托:
Then, inform the compiler that you are going to be a delegate for the other class by creating its instance, and setting yourself to be the delegate:
var secondVC : secondClass = secondClass()
secondClass.delegate = self
secondClass.callFromOtherClass() // This will call the method in the secondClass
// which will then ask its delegate to trigger a method valueChanged() -
// Who is the delegate? Well, your firstClass, so you better implement
// this method!
最后一件事是通过实施其方法来实际符合协议:
The last thing is to actually conform to the protocol by implementing its method:
func valueChanged() -> CGFloat {
return myVariable // which is 5 in your case (value taken from a question)
}
这会将 myVariable
值(本例中为5)分配给另一个值
class。
This will assign myVariable
value (5 in this example) to the value
in the other class.