TAP top app download banner
theAsianparent Singapore Logo
theAsianparent Singapore Logo
Product Guide
Sign in
  • Together Against RSV
  • SG60
  • Pregnancy
    • Due date calculator
    • I'm pregnant
    • Trying To Conceive
    • Labour
    • After birth
    • Baby loss
  • Parenting
    • Parent's Guide
    • Relationship & Sex
  • Child
    • Newborn
    • Baby
    • Toddler
    • Pre-Schooler
    • Kid
    • Pre-Teen & Teen
  • Feeding & Nutrition
    • Diseases-Injuries
    • Breastfeeding & Formula
    • Meal Planner
    • Health
    • Allergies & Conditions
    • Vaccinations
  • Education
    • Pre-School
    • Primary School
    • Secondary School
    • Primary School Directory
  • Lifestyle
    • Money
    • Travel & Leisure
    • Fashion
    • Home
    • Fitness
    • Contests & promotions
  • Events
  • Holiday Hub
  • Aptamil
    • Immunity
    • Intelligence
  • TAP Recommends
  • Shopping
  • Press Releases
  • Project Sidekicks
  • Community
  • Advertise With Us
  • Contact Us
  • VIP

Title goes here …………

4 min read
Title goes here …………

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

Partner Stories
Unlocking Financial Independence for the Next Generation
Unlocking Financial Independence for the Next Generation
Games to Play With Kids Indoors: 8 Fun Games You Can Play When You're Too Tired to Move
Games to Play With Kids Indoors: 8 Fun Games You Can Play When You're Too Tired to Move
How Stokke Products Can Be The Second-Best Gift To Parents After Giving Birth?
How Stokke Products Can Be The Second-Best Gift To Parents After Giving Birth?
What to Expect the First 24 Hours After Giving Birth
What to Expect the First 24 Hours After Giving Birth

Got a parenting concern? Read articles or ask away and get instant answers on our app. Download theAsianparent Community on iOS or Android now!

img
Written by

irai anbu

  • Home
  • /
  • Parenting
  • /
  • Title goes here …………
Share:
  • DeRAMA: Honouring motherhood through transformative postpartum care

    DeRAMA: Honouring motherhood through transformative postpartum care

  • Festive Buffets the Whole Family Will Love (Yes, Even the Kids)

    Festive Buffets the Whole Family Will Love (Yes, Even the Kids)

  • From Arcades to Cyberspace: Where Teens Hang Out Has Changed. Here’s How Parents Can Keep Them Safe

    From Arcades to Cyberspace: Where Teens Hang Out Has Changed. Here’s How Parents Can Keep Them Safe

  • DeRAMA: Honouring motherhood through transformative postpartum care

    DeRAMA: Honouring motherhood through transformative postpartum care

  • Festive Buffets the Whole Family Will Love (Yes, Even the Kids)

    Festive Buffets the Whole Family Will Love (Yes, Even the Kids)

  • From Arcades to Cyberspace: Where Teens Hang Out Has Changed. Here’s How Parents Can Keep Them Safe

    From Arcades to Cyberspace: Where Teens Hang Out Has Changed. Here’s How Parents Can Keep Them Safe

Get advice on your pregnancy and growing baby. Sign up for our newsletter
  • Pregnancy
  • Family Occasions
  • Lifestyle
  • Normal Delivery
  • Ages & Stages
  • Trying To Conceive
  • News
  • TAP Community
  • Advertise With Us
  • Contact Us
  • Become a Contributor


  • Singapore flag Singapore
  • Thailand flag Thailand
  • Indonesia flag Indonesia
  • Philippines flag Philippines
  • Malaysia flag Malaysia
  • Vietnam flag Vietnam
© Copyright theAsianparent 2026. All rights reserved
About Us|Privacy Policy|Terms of Use |Sitemap HTML
  • Tools
  • Articles
  • Feed
  • Poll

We use cookies to ensure you get the best experience. Learn MoreOk, Got it

We use cookies to ensure you get the best experience. Learn MoreOk, Got it