As we know Structs in Swift are Value Type Objects.Meaning when you assign or pass a Struct object to another property or argument it creates a copy of the original one.Let’s test it out://1struct User { var name: String}//2var user1 = User(name: "Mark")var user2 = user1//3user2.name = "John"print("user1 name: (user1.name)")print("user2 name: (user2.name)")We create a Struct named User (1).(2)We define a property user1 as User with name Mark.Then we define a property user2 equal to user1.We change user2 name to John (3)The print result is:user1 name: Markuser2 name: JohnAs we can see only user2 name has changed to John even if user2 property was created from user1. This is because a copy of user1 is created and passed to user2 value.Sometimes we may want to change this behaviour.In order to accomplish it we have to use a technique called Boxing.We will wrap the Struct instance inside a Class instance and share the Class Instance to be changed from other resources.Let’s do it://1final class UserBox { var user: User init(user: User) { self.user = user }}//2var user1 = User(name: "Mark")//3var userBox = UserBox(user: user1)First we create a new Class named UserBox (1).UserBox has 1 property of type User that been set from init method.Now lets say we have an instance of user1 that we want to share (2)We will wrap user1 inside userBox. (3)Now lets pass this instance://1var userBox2 = userBox//2userBox2.user.name = "Nick"print("userBox1 name: (userBox.user.name)")print("userBox2 name: (userBox2.user.name)")We define a property userBox2 to be equal to userBox (1).Then we change that name to Nick (2). As you can see to access the User we use the user property of UserBox.Now the magic happens… The print result is:userBox1 name: NickuserBox2 name: NickAs we can see both userBox1 and userBox2 user name property has changed.See you in the next story!!!As we know Structs in Swift are Value Type Objects.Meaning when you assign or pass a Struct object to another property or argument it creates a copy of the original one.Let’s test it out://1struct User { var name: String}//2var user1 = User(name: "Mark")var user2 = user1//3user2.name = "John"print("user1 name: (user1.name)")print("user2 name: (user2.name)")We create a Struct named User (1).(2)We define a property user1 as User with name Mark.Then we define a property user2 equal to user1.We change user2 name to John (3)The print result is:user1 name: Markuser2 name: JohnAs we can see only user2 name has changed to John even if user2 property was created from user1. This is because a copy of user1 is created and passed to user2 value.Sometimes we may want to change this behaviour.In order to accomplish it we have to use a technique called Boxing.We will wrap the Struct instance inside a Class instance and share the Class Instance to be changed from other resources.Let’s do it://1final class UserBox { var user: User init(user: User) { self.user = user }}//2var user1 = User(name: "Mark")//3var userBox = UserBox(user: user1)First we create a new Class named UserBox (1).UserBox has 1 property of type User that been set from init method.Now lets say we have an instance of user1 that we want to share (2)We will wrap user1 inside userBox. (3)Now lets pass this instance://1var userBox2 = userBox//2userBox2.user.name = "Nick"print("userBox1 name: (userBox.user.name)")print("userBox2 name: (userBox2.user.name)")We define a property userBox2 to be equal to userBox (1).Then we change that name to Nick (2). As you can see to access the User we use the user property of UserBox.Now the magic happens… The print result is:userBox1 name: NickuserBox2 name: NickAs we can see both userBox1 and userBox2 user name property has changed.See you in the next story!!!As we know Structs in Swift are Value Type Objects.Meaning when you assign or pass a Struct object to another property or argument it creates a copy of the original one.Let’s test it out://1struct User { var name: String}//2var user1 = User(name: "Mark")var user2 = user1//3user2.name = "John"print("user1 name: (user1.name)")print("user2 name: (user2.name)")We create a Struct named User (1).(2)We define a property user1 as User with name Mark.Then we define a property user2 equal to user1.We change user2 name to John (3)The print result is:user1 name: Markuser2 name: JohnAs we can see only user2 name has changed to John even if user2 property was created from user1. This is because a copy of user1 is created and passed to user2 value.Sometimes we may want to change this behaviour.In order to accomplish it we have to use a technique called Boxing.We will wrap the Struct instance inside a Class instance and share the Class Instance to be changed from other resources.Let’s do it://1final class UserBox { var user: User init(user: User) { self.user = user }}//2var user1 = User(name: "Mark")//3var userBox = UserBox(user: user1)First we create a new Class named UserBox (1).UserBox has 1 property of type User that been set from init method.Now lets say we have an instance of user1 that we want to share (2)We will wrap user1 inside userBox. (3)Now lets pass this instance://1var userBox2 = userBox//2userBox2.user.name = "Nick"print("userBox1 name: (userBox.user.name)")print("userBox2 name: (userBox2.user.name)")We define a property userBox2 to be equal to userBox (1).Then we change that name to Nick (2). As you can see to access the User we use the user property of UserBox.Now the magic happens… The print result is:userBox1 name: NickuserBox2 name: NickAs we can see both userBox1 and userBox2 user name property has changed.See you in the next story!!!
Title goes here …………
Loading...
You got lucky! We have no ad to show to you!
Loading...
You got lucky! We have no ad to show to you!
Loading...
You got lucky! We have no ad to show to you!