You can do this in different ways.. You can use an inout parameter on a method to allow the method to update the parameter, like this:
var aString = ""
func doStuffWithA(inout theString: String) {
theString = "Groovy"
}
doStuffWithA(&aString) // changes aString to "Groovy"
Or you can declare the property outside of the method:
class SomeClass {
var someString: String = ""
func doStuff() {
self.someString = "Groovy"
}
}
If you want this just for a segue, you can pass the object on performSegueWithIdentifier, like this:
func doStuff() {
var aString = "Groovy"
performSegueWithIdentifier("someSegue", sender: aString)
}
// Then here you can use it and assign it as a property on the next view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "someSegue" {
guard let aString = sender as? String else {
return
}
let nextVC = segue.destinationViewController as! SomeVC
nextVC.someProperty = aString
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…