Swift - 从函数返回初始化属性

时间:2015-02-24 04:17:57

标签: function swift properties declaration

我在另一个具有返回值的swift文件中有一个公共函数。我希望将这些返回值初始化为ViewController2中的属性。

公共职能

public func calculateBoundary (inout f:[Float], s:[Float], n:NSInteger) -> (forceBound1:Float, forceBound2: Float, displacement:[Float]) 

ViewController2

我希望在ViewController2类中使用 calculateBoundary 函数的返回值初始化3个属性。

    class ViewController2: UIViewController , UITableViewDelegate, UITableViewDataSource {

   // Values (f,s,n) are values that user input from another ViewController and then pass to this ViewController2.

// I'll then call the function _calcuateBoundary_ here, which passes the values (f,s,n) and then to calculate the return values here. 

    }

2 个答案:

答案 0 :(得分:0)

只需在calculateBoundary()的初始化程序中调用ViewController2方法即可。这是一幅草图:

  // public function - in any file
  func ret3 () -> (b1:Float, b2:Float, b3:Float) { 
      return (1, 2, 3) 
  } 

 // your `ViewController2`
 class VC2 { 
   var b1 : Float 
   var b2 : Float 
   var b3 : Float 
   init () { 
     (b1, b2, b3) = ret3()
   } 
 } 

这就是行动:

> var vc2 = VC2()
vc2: VC2 = {
  b1 = 1
  b2 = 2
  b3 = 3
}

答案 1 :(得分:0)

var forceB1 : Float = 0.0
var forceB2 : Float = 0.0
var springD : [Float] = []

override func viewDidLoad() {

(forceB1,forceB2,springD) = calculateBoundary(&f1View2,f2View2, springDView2)

}