[SWIFT] What is the difference between a class and a struct? ?? ??

Introduction

This time, I will summarize the differences between classes and structs as a memorandum.

Classes and structs

Classes and structs can be divided into value type and reference type depending on the value passing method.

The "type" here is not a String type or an Int type. It represents a mechanism in which how to handle an object is predetermined.

And the biggest difference between the two is whether to share changes with other variables or constants.

· Value types do not share changes`` (eg struct, enum) · Reference types share changes`` (eg class)

In fact, value types are not just structs, but also enums.

Value type

A type in which the instance represents the value itself, not a reference to it.

In other words, once assigned, the instance It has the advantage of being invariant unless reassigned and its value predictable.

Let's see how it works in code.

Let's see what happens to the passing of instances of a structure called Color type that expresses color in RGB type.

First, define a struct Color and prepare three variables. Then, assign the Color defined earlier to the variable a. Then assign a to the variable b to change the red value of a.

struct Color {
   var red: Int
   var blue: Int
   var green: Int
}

var a = Color(red: 255, blue: 0, green: 0) //Substitute red for variable a
var b = a //Substitute a for variable b

a.red = 0 //Change red of a to black

//Result ↓

//a turned black...
a.red // 0
a.blue // 0
b.green // 0

//b remains red!
b.red // 255
b.blue // 0
b.green // 0

Looking at the results, changing a has no effect on b. What you can see here is that no matter how much you change a, b is not affected.

From the above analogy, we can see that the value type is copying the value each time it is assigned to a variable / constant or passed to a function.

Reference type

A type in which the instance represents a reference to a value.

Unlike the value type, there is no copy of the instance when assigning to a variable / constant or when passing to a function, so there is an advantage that efficient passing to an instance is possible.

As an example, let's look at a reference type class that has an Int type value as a property.

Assign the IntBox class defined for the variable a, and assign a to the variable b.

Assigning a to the variable b means that the instance IntBox (value: 1) referenced by a is also referenced by b. It means that.

In other words, variables a and b have the same instance IntBox (value: 1).

So, if you change the value of variable a as shown below, the value of variable b will also change.

class IntBox {
  var value: Int

  //Initialize
  init(value: Int) {
    self.value = value
  }
}

var a = IntBox(value: 1) //a is IntBox (value):Refer to 1)
var b = a //b refers to the same instance as a

//Both are 1
a.value // 1
b.value // 1

// a.Substituting 2 for value...
a.value = 2

// b.The value of value also changes to 2!
a.value // 2
b.value // 2

How should I use value type and reference type properly?

So how do you use these two properly when actually developing? I don't know the details either, so I looked it up.

There were some articles that were easy to understand when I looked them up, so I will post them ↓ How to use structure and class properly in Swift (point) [Swift] Use of structure and class properly Note on how to use Swift classes and structures properly

end

If you find something wrong, please feel free to comment.

Recommended Posts

What is the difference between a class and a struct? ?? ??
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
What is the difference between a web server and an application server?
What is the difference between System Spec and Feature Spec?
[Rails] What is the difference between redirect and render?
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between skip and pending? [RSpec]
What is the difference between Java EE and Jakarta EE?
[Rails] What is the difference between bundle install and bundle update?
What is the difference between an action and an instance method?
[Java] What is the difference between form, entity and dto? [Bean]
What is a wrapper class?
What is the BufferedReader class?
Difference between class and instance
Difference between instance method and class method
Difference between interface and abstract class
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[Swift] What is "inheriting a class"?
What is the LocalDateTime class? [Java beginner] -Date and time class-
Jersey --What is Difference Between bind and bindAsContract in HK2?
Is there a performance difference between Oracle JDK and OpenJDK at the end of 2017?
What is the difference between the responsibilities of the domain layer and the application layer in the onion architecture [DDD]
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
About the difference between irb and pry
Easy to understand the difference between Ruby instance method and class method.
[Java] Understand the difference between List and Set
[iOS] Understand the difference between frame and bounds
[Rails / ActiveRecord] About the difference between create and create!
Understand the difference between abstract classes and interfaces!
What is a constructor?
What is a stream
Difference between vh and%
What is a Servlet?
Difference between i ++ and ++ i
Let's explain the difference between an interpreter and a compiler using a Venn diagram
[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
Shows how many years and months the difference from a particular date is
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
About the difference between classes and instances in Ruby
Calculate the difference between numbers in a Ruby array
Compare the difference between dockerfile before and after docker-slim
Difference between Spring AOP and library proxy target class
[Rails] I investigated the difference between redirect_to and render.
[Swift] UITextField taught me the difference between nil and ""
Understand in 3 minutes! A very rough explanation of the difference between session and cookie
Difference between product and variant
What is a boolean type?
What is a floating point?
Difference between redirect_to and render
What is the pluck method?
What is a meaningful comment?
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render