Detailed explanation of class and structure in Swift language _swift

Source: Internet
Author: User

Class
in Swift, classes are building flexible building blocks. Similar to constants, variables, and functions, the properties and methods of the class that the user can define. Swift provides us with a declaration class without the user creating interfaces and implementing the functionality of the file. Swift allows us to create classes as a single file and an external interface, which will be created by default in the class once initialized.

Benefits of using classes:

    • Inherit the property of a class to another class
    • Type conversions enable users to check the type of a class at run time
    • The initializer needs to dispose of the freed memory resource
    • Reference count allows class instances to have more than one reference

Common characteristics of classes and structs:

    • property is defined as storing the value
    • Subscript is defined to provide access value
    • method is initialized to improve functionality
    • The initial state is defined by the initialization function
    • function expanded to exceed default value
    • Confirm Protocol Functional Standard

Grammar

Copy Code code as follows:

Class ClassName {
Definition 1
Definition 2
---
Definition N
}

Defining classes
Copy Code code as follows:

Class student{
var studname:string
var mark:int
var mark2:int
}

Syntax for creating an instance:
Copy Code code as follows:

Let Studrecord = Student ()
Example
Class Marksstruct {
var mark:int
Init (mark:int) {
Self.mark = Mark
}
}

Class Studentmarks {
var mark = 300
}
Let Marks = Studentmarks ()
println ("Mark is \ (Marks.mark)")


When we use playground to run the above program, we get the following results

Mark is 300

Accessing class properties as reference types
Class properties can be accessed using the '. ' Syntax. The property name is detached from "." After the instance name.

Copy Code code as follows:

Class Marksstruct {
var mark:int
Init (mark:int) {
Self.mark = Mark
}
}

Class Studentmarks {
var mark1 = 300
var MARK2 = 400
var Mark3 = 900
}
Let Marks = Studentmarks ()
println ("Mark1 is \ (MARKS.MARK1)")
println ("Mark2 is \ (MARKS.MARK2)")
println ("Mark3 is \ (MARKS.MARK3)")


When we use playground to run the above program, we get the following results

MARK1 is
Mark2 are Mark3 is
900

Class identifier
Refer to a single instance of multiple constants and variables at Swift. To understand the use of constants and variables to point to a particular class instance identifier operator. Class instances are always passed by reference. The class Nsstring,nsarray and Nsdictionary instances are always assigned as references to existing instances instead of using a copy.

Copy Code code as follows:

Class Sampleclass:equatable {
Let myproperty:string
Init (s:string) {
MyProperty = S
}
}
Func = = (Lhs:sampleclass, rhs:sampleclass)-> Bool {
return Lhs.myproperty = = Rhs.myproperty
}

Let SpClass1 = SampleClass (s: "Hello")
Let SpClass2 = SampleClass (s: "Hello")

SpClass1 = = SPCLASS2//False
println ("\ (SPCLASS1)")

SpClass1!== SpClass2/True
println ("\ (SPCLASS2)")


When we use playground to run the above program, we get the following results

Main. SampleClass
Main. SampleClass

Structural body
Swift provides a flexible building block that utilizes constructs as a structure. By taking advantage of these structures, you can define the methods and properties of the builder at once.

This differs from C and objective C programming:

Structs do not require the implementation of files and interfaces.

The structure allows us to create a file and automatically extend its interface to other blocks.

The values of the variables in the structure are copied and passed on to the subsequent code, which cannot be changed by a copy of the old values returned.

Grammar

Copy Code code as follows:

Structures are defined with a ' Struct ' Keyword.
struct Namestruct {
Definition 1
Definition 2
---
Definition N
}

Definition of the structure body
Consider for example, suppose you want to access a student that contains three account record marks and find the sum of three accounts. Here, the markstruct is used to initialize the structure with three tags, and the data type is ' Int '.
Copy Code code as follows:

struct markstruct{
var mark1:int
var mark2:int
var mark3:int
}

Accessing Structures and properties
A member of a struct is accessed by its structure name. The ' let ' keyword is initialized in the instance of the structure body.
Copy Code code as follows:

struct Studentmarks {
var mark1 = 100
var MARK2 = 200
var MARK3 = 300
}
Let Marks = Studentmarks ()
println ("Mark1 is \ (MARKS.MARK1)")
println ("Mark2 is \ (MARKS.MARK2)")
println ("Mark3 is \ (MARKS.MARK3)")

When we use playground to run the above program, we get the following results:

MARK1 is
Mark2 are Mark3 is
300

Student scores need to access the structure name "Studentmarks". Struct members are initialized to Mark1, MARK2, mark3 integer type values. The struct Studentmarks () is then passed to the ' marks ' using the ' let ' keyword. ' Marks ' will contain the value of the struct member. It now accesses the value of the initial name of the structure by '. ' and prints it.

Copy Code code as follows:

struct Marksstruct {
var mark:int

Init (mark:int) {
Self.mark = Mark
}
}
var astruct = marksstruct (mark:98)
var bstruct = astruct//astruct and Bstruct are two structs with the same value!
Bstruct.mark = 97
println (Astruct.mark)//98
println (Bstruct.mark)//97


When we use playground to run the above program, we get the following results:


97

The best way to use a structural body
The Swift language provides functionality to define the structure body as a custom data type for building functional blocks. The value of an instance of a struct is passed to the definition block for further action.

Need to have structure:

Encapsulates a simple data value

Use value instead of reference to copy the associated properties that encapsulate the data to it

struct body is copy and reference

In Swift, a struct is the value of its members, not its reference.

Copy Code code as follows:

struct markstruct{
var mark1:int
var mark2:int
var mark3:int

Init (Mark1:int, Mark2:int, Mark3:int) {
SELF.MARK1 = Mark1
SELF.MARK2 = Mark2
SELF.MARK3 = Mark3
}
}

var marks = markstruct (mark1:98, mark2:96, mark3:100)
println (MARKS.MARK1)
println (MARKS.MARK2)
println (MARKS.MARK3)


When we use playground to run the above program, we get the following results:

100

Another example

Copy Code code as follows:

struct markstruct{
var mark1:int
var mark2:int
var mark3:int

Init (Mark1:int, Mark2:int, Mark3:int) {
SELF.MARK1 = Mark1
SELF.MARK2 = Mark2
SELF.MARK3 = Mark3
}
}

var fail = markstruct (mark1:34, mark2:42, mark3:13)

println (FAIL.MARK1)
println (FAIL.MARK2)
println (FAIL.MARK3)


When we use playground to run the above program, we get the following results:

13

The

struct "markstruct" requires its members to be defined first: Mark1, MARK2, and Mark3. Now, the variable initialization of the member class holds the integer value. A copy of the struct member is then created using the "self" keyword. When a copy of a struct member creates a structure block, its parameter tag is passed to the ' marks ' variable, which now holds the student's score. Then the markup prints 98, 96, 100. The next step is for another instance of the same struct member to be named ' fail ' to point to a member of the same structure with different tags. Then the tag is now printed as 34, 42, 13. This clearly shows that the structure will have a copy of the member variables, incoming members to the feature blocks they are about to launch.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.