function - Swift - 更改函数调用的数组值

标签 function swift xcode6

我想为我的函数调用更改数组值。

调用函数计算的 ViewController2 代码

class ViewController2: UIViewController , UITableViewDelegate, UITableViewDataSource {

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

}

函数计算代码

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])          
        }
}

我有一个错误:'@lValue $T5' 与'Float' 不同,当我想更改我的 f[rows] = 0.0

有什么建议吗?

最佳答案

这是因为f参数是一个不可变数组。您可以使用函数签名将其更改为 可变数组 参数,如下所示

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

如果您还想修改传递给函数的原始数组,则需要使用inout 参数。

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

你可以这样调用它

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

let result = calculateBoundary(&f, &g, 1)

关于function - Swift - 更改函数调用的数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28468669/

相关文章:

c - 我需要有关 C 中字符串的帮助

swift - 如何将 CFData 传递给 C char**?

ios - 将项目引用添加到 Swift iOS XCode 项目并调试

ios - 启动 Image Xcode 6 iOS 7 警告

c++ - 与类成员的函数调用?

c++ - LNK2005 - 在 .hpp 文件中声明函数时出错

javascript - 我可以从 document.formID.submit(); 调用 JS 函数吗?

iOS 后台 App Fetch 不工作

swift - 如何从 Swift 中的字符串中删除变音符号?

ios - 奇怪的 "Crash"字体添加到 Info.plist