TAP top app download banner
theAsianparent
theAsianparent
Product Guide
  • Together Against RSV
  • SG60
  • Pregnancy
  • Parenting
  • Child
  • Feeding & Nutrition
  • Education
  • Lifestyle
  • Events
  • Holiday Hub
  • Aptamil
  • TAP Recommends
  • Shopping
  • Press Releases
  • Project Sidekicks
  • Community
  • Advertise With Us
  • Contact Us
  • VIP
Login
    • Articles
  • Together Against RSVTogether Against RSV
  • SG60SG60
  • PregnancyPregnancy
  • ParentingParenting
  • ChildChild
  • Feeding & NutritionFeeding & Nutrition
  • EducationEducation
  • LifestyleLifestyle
  • EventsEvents
  • Holiday HubHoliday Hub
  • AptamilAptamil
  • TAP RecommendsTAP Recommends
  • ShoppingShopping
  • Press ReleasesPress Releases
  • Project SidekicksProject Sidekicks
  • CommunityCommunity
  • Advertise With UsAdvertise With Us
  • Contact UsContact Us
  • VIPVIP
    • Community
  • Poll
  • Photos
  • Food
  • Recipes
  • Topics
  • Read Articles
    • Tracker
  • Pregnancy Tracker
  • Baby Tracker
    • Rewards
  • RewardsRewards
  • Contests
  • VIP ParentsVIP Parents
    • More
  • Feedback

Privacy PolicyCommunity GuidelinesSitemap HTML

Download our free app

google play store
app store

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

Feed

Feed

Get tailored articles about parenting, lifestyle, expert opinions right at your fingertips

Poll

Poll

Participate in interesting polls and see what other parents think!

Photos

Photos

Share the photos of loved ones in a safe, secure manner.

Topics

Topics

Join communities to bond with fellow mums and dads.

Tracker

Tracker

Track your pregnancy as well as baby’s development day-by-day!

theAsianparent

Download our free app

Google PlayApp Store

Mums around the world

Singapore flag
Singapore
Thailand flag
Thailand
Indonesia flag
Indonesia
Philippines flag
Philippines
Malaysia flag
Malaysia
Vietnam flag
Vietnam

Partner Brands

Rumah123VIP ParentsMama's ChoiceTAP Awards

© 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