gpt4 book ai didi

ios - 更新数组索引但它更改了 IOS 中的所有索引

转载 作者:行者123 更新时间:2023-11-28 23:57:13 29 4
gpt4 key购买 nike

这里我试图更新索引中的特定值,但它更新了所有索引

class salarty  {

var sal_id :String = ""
var sal_amount :String = ""
var sal_iScreated :Bool = false

init(sal_id : String, sal_amount : String, sal_iScreated: Bool){

self.sal_id = sal_id
self.sal_amount = sal_id
self.sal_iScreated = sal_id

}
}

class Employee {

let id: Int, firstName: String, lastName: String
var salaryArray :[salarty] = [salarty]()


init(id: Int, firstName: String, lastName: String, salaryArray: [salarty]) {
self.id = id
self.firstName = firstName
self.lastName = lastName
self.salaryArray = salaryArray
}
}


array_SalaryDetails.append(salarty(sal_id : "2", sal_amount : "3000", sal_iScreated: false))

let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet",salaryArray :array_SalaryDetails),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov",salaryArray :array_SalaryDetails),
Employee(id: 4, firstName: "Hans", lastName: "Passant",salaryArray :array_SalaryDetails)
]

我在这里尝试了一些代码,但它不起作用

方法一:

var model  = Employee[(indexPath?.section)!].salaryArray
let model2 = model[(indexPath?.row)!]
model = model.map{
let mutableval = $0
if $0.sal_id == model2.sal_id {
mutableBook.sal_iScreated = !model2.sal_iScreated
}
return mutableval
}

方法二:

    let model  = array_Main[(indexPath?.section)!].serviceArray
let model2 = model[(indexPath?.row)!]

model2.service_isSelected = !model2.service_isSelected

最佳答案

那是因为在 Swift 类实例中,通过引用传递,而不是通过值传递。

因此,您创建并传递给多个 Employee 对象的薪水数组最终引用了相同的薪水对象。因此,更改任何 Employee 对象中的任何数组中的薪水值都会反射(reflect)所有 Employee 对象中的更改

两种可能的解决方案:

解决方案 1:首选解决方案

salarty 更改为 struct 而不是 class 并且如果可能的话也更改 salary 的拼写 :P

struct salarty  {

var sal_id :String = ""
var sal_amount :String = ""
var sal_iScreated :Bool = false

init(sal_id : String, sal_amount : String, sal_iScreated: Bool){

self.sal_id = sal_id
self.sal_amount = sal_id
self.sal_iScreated = sal_iScreated

}
}

解决方案 2:

在分配之前为每个员工创建工资数组的深拷贝。

class salarty : NSObject, NSCopying  {
func copy(with zone: NSZone? = nil) -> Any {
let copy = salarty(sal_id: self.sal_id, sal_amount: self.sal_amount, sal_iScreated: self.sal_iScreated)
return copy
}


var sal_id :String = ""
var sal_amount :String = ""
var sal_iScreated :Bool = false

init(sal_id : String, sal_amount : String, sal_iScreated: Bool){

self.sal_id = sal_id
self.sal_amount = sal_id
self.sal_iScreated = sal_iScreated

}
}

对于每个员工,您可以使用以下任一方法创建一个新数组

    array_SalaryDetails.append(salarty(sal_id : "2", sal_amount : "3000", sal_iScreated: false))
//salary array copy for first employee
var firstEmployeearrayCopy = [salarty]()
for salary in array_SalaryDetails {
firstEmployeearrayCopy.append(salary.copy() as! salarty)
}
Employee(id: 1, firstName: "Jon", lastName: "Skeet",salaryArray : firstEmployeearrayCopy)

//salary array copy for second employee
var secondEmployeearrayCopy = [salarty]()
for salary in array_SalaryDetails {
secondEmployeearrayCopy.append(salary.copy() as! salarty)
}
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov",salaryArray :secondEmployeearrayCopy)

或者您可以为每个员工再次创建数组。

在 swift 中,Array按值传递,但数组中的对象(类的实例)仍将按引用传递你将不得不做所有这些变通办法:)

一条建议:

类名应该以大写字母开头。只有变量名遵循驼峰式大小写。

希望对你有帮助

关于ios - 更新数组索引但它更改了 IOS 中的所有索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50852073/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com