arrays - 快速更新数组的元素值
问题描述
我有很多使用 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 会被更新,但事实并非如此。阵列总是恢复到其初始状态。
NB 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 而无需创建新变量来记录每次更改?这是我应该考虑的问题,还是在更一般的层面上,我应该采取更好的策略?
我很惊讶我为处理这个问题付出了这么多努力。我认为如果我阅读文档的相关部分并研究一些示例代码,答案就会很清楚。我发现的所有示例代码都过于简单,只关注更新数组的一次迭代。
任何意见或建议将不胜感激!
解决方案
感谢您的回复巴文。通过将 responseArray 通过引用(如 Bhavin 建议)传递给必要的类(结果是 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"]
我仍然不确定如何在我正在从事的项目中实施此解决方案。我将为此创建一个单独的问题,因为它似乎引发了不同的问题。
推荐阅读
- hashicorp-vault - 为什么创建令牌会导致“父令牌查找失败”
- oracle-adf - af:table 在将数据添加到表后未对齐标题
- python - PyQt:事件队列大小
- arduino - 使用 aws-sdk-arduino 在 Arduino 上批量写入 dynamoDB
- directx - 如何使用代码块设置 directX
- laravel - Laravel 对待 www 和非 www 不同
- oauth-2.0 - 认证后与 API 交互以获取用户信息
- excel - Excel 运行时错误 1004
- java - 我们可以在处理中创建部分彩色文本吗?
- server - 我可以恢复我从 WHM 中删除的交流面板吗