Reputation: 13
I'm a beginner of swift.
I wrote code and one question.
I want to get variable b
from func A
but I don't know how.
How to get it.
/*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*/)
}
}
Upvotes: 0
Views: 454
Reputation: 24341
You can simply use a Tuple
of type (CGFloat, CGFloat)
to achieve that, i.e.
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)
}
Upvotes: 1
Reputation: 271040
Note that the actual solution to your problem depends highly on what you actually want to do (your ultimate goal).
You can't access a
or b
in B
because a
and b
are in a different scope from B
. You can't access local variables declared in a function from another function.
To access them, you need to move a
and b
to a scope that is accessible by B
. In this case, this can be the scope of the extension:
extension FirstViewController{
var a: CGFloat { return view.frame.size.width }
var b: CGFloat { return view.frame.size.height }
private func A() {
}
private func B() {
self.Something.frame.size = CGSize(width: a, height: b)
}
}
Upvotes: 0