简体   繁体   English

实体数组不会迅速变异吗?

[英]Array of entity does not mutate in swift?

User Entity Model- 用户实体模型

class UserEntity: NSObject {

    var isAlreadyUser:Bool

    init(isAlerdy:Bool){
        isAlreadyUser = isAlerdy
    }

}

App Delegate / Global Array 应用程序委托/全局数组

let new = ["F","E","D","C","B","A"]
        for obj in new{
            arrUser.append(UserEntity(isAlerdy: false))
        }

VIEW CONTROLLER 视图控制器

   let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    let home = Array(appDelegate.arrUser)

    home[0].isAlreadyUser = true

    print(appDelegate.arrUser[0].isAlreadyUser)

After I edit the local home array and update isAlreadyUser to true from false. 在我编辑本地home数组并将isAlreadyUser从false更新为true之后。 This also changes in global array. 这也在全局数组中发生了变化。 Even I am mutating any making a copy of global array it still changes it i both the array. 即使我正在变异任何复制全局数组的副本,它仍然会在两个数组中对其进行更改。

I think some thing is wrong with entity. 我认为实体有些问题。 It is strong and not changing according to local scope of array. 它很强,并且不会根据数组的局部范围而变化。

Help me out. 帮帮我。

EDIT: 编辑:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        var areAlreadyUsers:[UserEntity] = []

        areAlreadyUsers = Array(appDelegate.arrUser)

        areAlreadyUsers[0].isAlreadyUser = true

        print(appDelegate.arrUser[0].isAlreadyUser)

Still no help. 仍然没有帮助。 My global array is still changing. 我的全局阵列仍在变化。

If you were to make the UserEntity into a struct, you would not have the problem. 如果您要使UserEntity成为一个结构,就不会有问题。 Does it need to be an NSObject if not try using struct instead of class. 如果不尝试使用struct而不是class的话,它是否需要为NSObject This way you get copy on write behavior. 这样,您就可以复制写入行为。

Problem: 问题:

The elements of the array are referenceType hence only the pointers to the objects will be copied. 数组的元素是referenceType,因此将仅复制指向对象的指针。 Hence modifying them will reflect in both the places. 因此,修改它们将在两个地方都反映出来。

Solution: 解:

  1. Copy each item contained in the array. 复制数组中包含的每个项目。

    you can do some thing like, let localArray = globalArray.map { $0.copy() } . 您可以执行以下操作, let localArray = globalArray.map { $0.copy() } Please note that it is important your object should implement any of the copy methods such as copyWithZone . 请注意,您的对象应实现任何复制方法(例如copyWithZone ,这一点很重要。

  2. Use a value type such as struct instead of class . 使用诸如structclass的值类型代替class

The point is that arrUser and home just contain pointers to the same UserEntity objects, which are unique (not copied). 关键是arrUserhome仅包含指向相同UserEntity对象的指针,这些对象是唯一的(未复制)。 So what you do you just change a value of a UserEntity property and it will obviously be reflected everywhere. 因此,您只需要更改UserEntity属性的值UserEntity ,它显然会在所有地方得到反映。

What you want to do is to rethink your app architecture, why would you mutate this value at all? 您想要做的是重新考虑您的应用程序架构,为什么您要彻底改变此值? For instance, you can create an array 例如,您可以创建一个数组

var areAlreadyUsers:[UserEntity] = []

and just save there pointers to users that you would give true value. 并只保存指向用户的指针,您将获得true价值。 This way this information isn't saved anywhere. 这样,此信息就不会保存在任何地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM