更新数组索引,但它改变了IOS中的所有索引

时间:2018-06-14 07:37:46

标签: ios swift3

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

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

这里我尝试了一些不起作用的代码

方法1:

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
        }

方法2:

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

    model2.service_isSelected = !model2.service_isSelected

1 个答案:

答案 0 :(得分:1)

多数民众赞成因为Swift类的实例通过引用传递而不是按值

因此,您创建并传递给多个Employee对象的salary数组最终引用了相同的数组。因此,在任何Employee对象中更改数组的值将反映所有Employee对象的更改

两种可能的解决方案:

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

salarty更改为struct而非class,如果可能,也会更改薪水的拼写: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:

在分配之前为每个Employee创建一个工资数组的深层副本。

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)

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

在swift 数组 按值传递但数组中的对象仍然通过引用传递,因此您必须执行此操作所有这些解决方法:)

建议:

班级名称应以大写字母开头。只有变量名称跟随骆驼套管。

希望有所帮助