我是 swift 的初学者。
我写了代码和一个问题。
我想从 func A
获取变量 b
但我不知道如何。
如何获得。
/*This is extension from FirstViewController*/
extension FirstViewController{
private func A() {
let a:CGFloat = view.frame.size.width
let b:CGFloat = view.frame.size.height
}
private func B() {
self.Something.frame.size = CGSize(width: /*I want to get a in here*/, height: /*I want to get b in here*/)
}
}
最佳答案
您可以简单地使用 (CGFloat, CGFloat)
类型的 Tuple
来实现这一点,即
private func A() -> (a: CGFloat, b: CGFloat)
{
let a:CGFloat = view.frame.size.width
let b:CGFloat = view.frame.size.height
return (a, b)
}
private func B()
{
self.Something.frame.size = CGSize(width: self.A().a, height: self.A().b)
}
关于Swift 如何从同一个类中的另一个方法获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48112879/