Copying Classes

#copyingclass #copy

Overview

class User {
    var username = "Anonymous"
    
// creating a function to copy
    func copy() -> User {
        let user = User()
        user.username = username
        return user
    }
    
}

var user1 = User()
var user2 = user1.copy() // copying user1
user2.username = "Taylor"
var user3 = user2

print(user1.username) // print: "Anonymous"
print(user2.username) // print: "Taylor"
print(user3.username) // print: "Taylor"

Sources

Sample Codes

Sources

Videos

Articles / Documents

Last updated