This time, I learned about the differences in protocols, classes, and structures, so I will output them.
A literal translation of a protocol means something like a "treaty," "protocol," or "covenant." In the Swift language, it means "every promise between the developer and the Swift language".
First, let's see how to write a declaration.
qiita.rbvar
protocol <Protocol name> {
}
Basically, you declare it like this. The only difference from class is that the keyword "class" has become "protocol". Let's dig deeper.
From here, the clear difference between the protocol and the class is that the *** protocol does not write the contents of the method when writing the method. *** *** The writing method is as follows.
qiita.rbvar
protocol <Protocol name> {
func<Method name>()
}
*** At first glance, the format is similar to a class, but there is no {} after the method name ()! *** ***
The fact that the protocol does not write the contents of the method means that it does not make sense to just declare it.
Let's go deep into how to use it. Basically, the protocol is used as a set with "class".
qiita.rbvar
class <name of the class> <Protocol name> {
}
Write the protocol name after the ":" in the class declaration. This way of writing a protocol in a class is called *** ratification ***. If you ratify a protocol, you must implement the methods defined in that protocol. If it is not implemented, an error will occur.
Next, let's look at it concretely.
qiita.rbvar
protocol MorningProtocol {
func morningAction()
}
qiita.rbvar
class Date:MorningProtocol {
func morningAction(){
print("Eat breakfast")
print("Brush your teeth")
}
In the above, what to do after waking up in the morning is output to the debug area. This is the most basic use of the protocol
The protocol has a function as a type. When declaring a variable name, if you specify the protocol name by adding ":" after the variable name, the variable becomes a box dedicated to the class that has ratified the protocol.
qiita.rbvar
var <Variable name>:<Protocol name> = <name of the class> ()
The class is interpreted as a template. Classes are often used as an example to imagine the mold used to make Taiyaki.
The class declaration is as follows.
qiita.rbvar
class <name of the class> ()
The declaration method is as follows
qiita.rbvar
class <name of the class> {
func<Method name>(){}
}
Next, let's declare concretely
qiita.rbvar
class Hamburger {
var nakami = "Patty"
func sayNakami () {
print("The contents are"+nakami+"is") //The contents areパティis
}
Now let's actually create a class using the class created above.
qiita.rbvar
var hamburger = Hamburger()
hamburger.nakami = "Patty and cheese"
hamburger.sayNakami() //The contents are patties and cheese.
In the above example, an instance is created based on the class, and the instance is operated and processed. Cheese is added to the value of the nakami property to change the string.
In a nutshell, the difference between classes and protocols A class that cannot be inherited!
The basic writing method is as follows.
qiita.rbvar
struct structure name{
var property name: property type=formula
let property name: property type=formula
}
Let's look at a concrete example next
qiita.rbvar
struct SomeStruct {
var num1 = 246
let num2 = 135
}
let someStruct = SomeStruct()
let a = someStruct.num1 //246
let b = someStruct.num2 //135
In the above, I wrote that the difference between a class and a structure is whether it can be inherited or not, but let's look at a concrete example here.
qiita.rbvar
//Structure(Value type)
struct ColorStruct {
var color:String
}
//class(Reference type)
class ColorClass {
var color:String
init(color:String){
self.color = color
}
}
var colorStruct1 = ColorStruct(color: "Blue")
var colorStruct2 = colorStruct1
colorStruct2.color = "black"
println("colorStruct1:\(colorStruct1.color)、colorStruct2:\(colorStruct2.color)")
For output result structure, colorStruct2.Changing color does not change colorStruct1
"colorStruct1:Blue, colorStruct1:black"
var colorClass1 = ColorClass(color: "Blue")
var colorClass2 = colorClass1
cardClass2.color = "black"
println("colorClass1:\(colorClass1.color)、colorClass2:\(colorClass2.color)")
For the output result structure, colorClass2.If you change color, colorClass1 will also change
"colorClass1:black, colorClass2:black"
As mentioned above, we can see that the structures are not inherited and have independent concepts one by one.
When I was learning this time, the difference between the protocol, the class, and the structure was ... "What? How was it?", So I summarized it roughly!
Recommended Posts