快速更新数组的元素值

时间:2018-08-09 14:06:57

标签: arrays swift

我有使用Matlab的丰富经验,但是直到最近我才开始使用Swift 4进行编程。我当前的项目涉及构建调查表。我已使用Xcode中的“拖放”功能为情节提要中的按钮生成@IBAction函数,然后可以导致按下的按钮更改其外观。此功能包含在下面的代码片段的ButtonResponse类中:

    struct ResponseProfile {

       var responseArray: Array<String>
       init(responseArray: Array<String>) {
            self.responseArray = ["unchecked","unchecked","unchecked","unchecked","unchecked","unchecked","unchecked"]
        }

        mutating func updateArray(_ anArray: Array<String>) -> (Array<String>) {
            responseArray = anArray
            return responseArray
        }

    }

    class ButtonResponse: UIButton {

        var responseVariables: ResponseProfile

        var checkedImage = UIImage(named: "checkedResponseBox")! as UIImage
        var uncheckedImage = UIImage(named: "uncheckedResponseBox")! as UIImage

        required init?(coder aDecoder: NSCoder) {
            self.responseVariables = ResponseProfile(
                responseArray: []
            )
            super.init(coder: aDecoder)
        }


        @IBAction func checkboxTapped(_ sender: UIButton) {
            switch sender.accessibilityIdentifier {
                case "excellent":

                    let oldResponseStatus = responseVariables.responseArray[0]
                    if oldResponseStatus == "unchecked"{
                        sender.setImage(checkedImage, for: UIControlState.normal)
                        let oldResponsePresence = responseVariables.responseArray.contains("checked")

                        if oldResponsePresence == true {
                            responseVariables.responseArray = ["unchecked","unchecked","unchecked","unchecked","unchecked","unchecked","unchecked"]
                        }
                        responseVariables.responseArray[0] = "checked"
                    } else if oldResponseStatus == "checked" {
                        sender.setImage(uncheckedImage, for: UIControlState.normal)
                        responseVariables.responseArray[0] = "unchecked"
                }

                case "veryGood":
                    let oldResponseStatus = responseVariables.responseArray[1]
                    if oldResponseStatus == "unchecked" {
                        sender.setImage(checkedImage, for: UIControlState.normal)
                        let oldResponsePresence = responseVariables.responseArray.contains("checked")
                        if oldResponsePresence == true {
                            responseVariables.responseArray = ["unchecked","unchecked","unchecked","unchecked","unchecked","unchecked","unchecked"]
                        }
                        responseVariables.responseArray[1] = "checked"
                    } else if oldResponseStatus == "checked" {
                        sender.setImage(uncheckedImage, for: UIControlState.normal)
                        responseVariables.responseArray[1] = "unchecked"
                    }

                default: break

            }
        }

    }

我想象过我可以使用一个数组来内部表示用户界面中按钮的状态(这就是“ responseArray”变量)。通过在按下按钮后更改responseArray中的元素,我认为我可以跟踪按下了哪些按钮,并确保一次只检查一个按钮。我错误地认为responseArray将被更新,但事实并非如此。阵列始终恢复为其初始状态。

由于有七个响应选项,所以responseArray包含七个元素。到目前为止,我仅尝试对两个响应选项进行编程:“优秀”和“非常好”。

在尝试找到解决方案时,我试图在操场上简化以上代码:

import UIKit

    struct ResponseProfile {
        var responseArray: Array<String>
        init(responseArray: Array<String>) {
            self.responseArray =     ["unchecked","unchecked","unchecked","unchecked","unchecked","unchecked","unchecked"]
        }
        mutating func updateArray(input anArray: Array<String>) -> (Array<String>) {
            responseArray = anArray
            return responseArray
        }
    }

    class ButtonResponse {
        var responseVariables: ResponseProfile
        init(){
            self.responseVariables = ResponseProfile(responseArray: [])
        }
        var responseA = ResponseProfile(responseArray: [])

    }

    var responseOne = ResponseProfile(responseArray: [])
    responseOne.responseArray[0] = "checked" //user performs action resulting in first element being changed from a starting value of "unchecked" to "checked"
    responseOne.updateArray(input: responseOne.responseArray)

    var responseTwo = ResponseProfile(responseArray:[])
    responseTwo.responseArray //responseArray revert to initialization values. How can I keep changes to the responseArray?

如何在无需创建新变量来记录每个更改的情况下更新ResponseProfile结构中的responseArray?这是我应该关注的问题,还是从更一般的角度来看,我应该采取的更好的策略?

我为解决这个问题付出了如此多的努力,我感到很惊讶。我认为,如果我阅读了文档的相关部分并研究了一些示例代码,答案将会很清楚。我发现的所有示例代码都过于简单,只关注于更新数组的一次迭代。

任何评论或建议将不胜感激!

2 个答案:

答案 0 :(得分:0)

查看您的运动场代码,我发现您在初始化过程中将空白[]数组传递给ResponseProfile结构的参数。并且总是在初始化您的responseArray。

如果您想通过引用传递这些内容,您可以将“响应”配置文件更改为课程 在那里,您可以实现类似的功能,并使用inout参数保留相同的数组,而无需使用updateArray函数。

我在这里显示的示例是针对该类的,该类的对象可以通过引用传递。因此保留您以前的更改。

var responseTwo = ResponseProfile(responseArray:[])

如果要保留旧的响应,可以将该数组作为参数传递

var responseTwo = ResponseProfile(responseArray:responseOne.responseArray)

OR

var responseTwo = responseOne

将保留responseArray。

您可以在官方blog

了解更多信息

此外,您可以this post对案件有更多了解。

希望有帮助。

答案 1 :(得分:0)

感谢您的回应。通过按引用(如Bhavin建议)将responseArray传递到必要的类(事实证明这是ButtonResponse类,而不是ResponseProfile),我可以给responseArray一个初始值。然后,我使用buttonPress函数更新responseArray。见下文:

class ButtonResponse: Responses {
var responseArray: [String]
init (responseArray: [String]) {
    self.responseArray = responseArray
}

func buttonPress(inputString: String, targetIndex: Int) -> [String] {
    //need guard statement to block function when targetIndex > number of elements in responseArray
    responseArray[targetIndex] = inputString
    return responseArray
}
}

let initiateResponseArray = 

["unchecked","unchecked","unchecked","unchecked","unchecked","unchecked","unchecked"]
var doPress = ButtonResponse(responseArray: initiateResponseArray)
doPress.buttonPress(inputString: "checked", targetIndex: 0)
var getPressInfo1 = doPress.responseArray
print(getPressInfo1)
//prints: ["checked", "unchecked", "unchecked", "unchecked", "unchecked", "unchecked", "unchecked"]
doPress.buttonPress(inputString: "checked", targetIndex: 1)
var getPressInfo2 = doPress.responseArray
print(getPressInfo2)
//prints: ["checked", "checked", "unchecked", "unchecked", "unchecked", "unchecked", "unchecked"]

我仍然不确定如何在我正在从事的项目中实施此解决方案。我将为此创建一个单独的问题,因为它似乎提出了不同的问题。