3

I like to change an array value for my function call.

Code for my ViewController2 that calls a function calculate

class ViewController2: UIViewController , UITableViewDelegate, UITableViewDataSource {

var springDisplacement : [Float] {return calculate(forceView2, stiffView2, springNumView2) }

}

Code for function calculate

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

    println("\nForces in the array are")
    for rows in 0...n{
        if(rows==0||rows==n)
        { f[rows] = 0.0 }                
            println(f[rows])          
        }
}

I have an error: '@lValue $T5' is not identical to 'Float', when i want to change my f[rows] = 0.0

Any advices?

2 Answers 2

4

This is because f parameter is an immutable array. You can change this to a mutable array parameter by using the function signature as follows

func calculateBoundary (var f:[Float], var s:[Float], n:NSInteger)  -> (forceBound1:Float, forceBound2: Float, displacement:[Float]) { // }

If you want to also modify the original arrays passed to the function, you need to use inout parameters.

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

You can call it like this

var f : [Float] = [1.0,2.0]
var g : [Float] = [1.0,2.0]

let result = calculateBoundary(&f, &g, 1)
Sign up to request clarification or add additional context in comments.

Comments

1

f array is not mutable. You need to pass the f array as inout parameter:

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

Then you can call calculateBoundary like this:

var springDisplacement : [Float] {return calculate(&forceView2, stiffView2, springNumView2) }

1 Comment

var springDisplacement : [Float] {return calculate(&forceView2, stiffView2, springNumView2) } gives me an error: 'inout [(Float)]' is not convertible to '[Float]'. I have declared my f and forceView2 to be array of type float.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.