1

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. 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.

2
  • what exactly are you trying to do by mutating this value? Commented Jan 19, 2016 at 16:17
  • @tmac_balla I understand, How to make a copy of UserEntity Object?? Commented Jan 19, 2016 at 16:47

3 Answers 3

1

If you were to make the UserEntity into a struct, you would not have the problem. Does it need to be an NSObject if not try using struct instead of class. This way you get copy on write behavior.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. That solved my problem. But why would struct copy it and not NSObject??
@TusharLimaye it is that a Struct has copy on write semantics and a Class has reference semantics. These are fundamental aspects of these language elements.
1

Problem:

The elements of the array are referenceType hence only the pointers to the objects will be copied. 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() }. Please note that it is important your object should implement any of the copy methods such as copyWithZone.

  2. Use a value type such as struct instead of class.

3 Comments

second option is working. but the first one is not. There is not methods as copy in $0. Any why do we use copywithzone for these?
$0 is nothing but the objects inside the array. So you are actually invoking the UserEntity object. Since it is inherited from NSObject you can use any of the NSCopying protocol methods to facilitate "deep copying".
ohk Thanks, but I am not getting any NSObjects options for $0. I am just able to access properties in UserEntity.
0

The point is that arrUser and home just contain pointers to the same UserEntity objects, which are unique (not copied). So what you do you just change a value of a UserEntity property and it will obviously be reflected everywhere.

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. This way this information isn't saved anywhere.

5 Comments

I tried adding that but my issue is when I am "home[0].isAlreadyUser = true" doing this. My Global array also changes. How can I give this entity different memory location so that my global array does not changes. I hope I explained it.
@TusharLimaye That is not possible, because those objects are unique. You only can create an exact same object, but you don't need to do that, because it violates OOP principles. The option I gave you is the only good practice.
@TusharLimaye So if you want to know whether user isAlreadyUser, you just check that array and remove isAlreadyUser property at all.
Ok I understand it. But can we not have deep copy it and change the memory location of the other one. This is doing shallow copy that is the reason why my global array objects are also changing.
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate var areAlreadyUsers:[UserEntity] = [] areAlreadyUsers = Array(appDelegate.arrUser) areAlreadyUsers[0].isAlreadyUser = true print(appDelegate.arrUser[0].isAlreadyUser)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.