Learn from the zero-based Swift learning note (Day 30)-Choose a class or a struct?

Source: Internet
Author: User

Tag: "Study from scratch" study notes

Original article, welcome to reprint. Reprint please indicate: Guan Dongsheng's blog

    Classes and structures are very similar, and in many cases there is no difference. If you are a designer when designing a system, do you design a certain type as a class or a structure?

Similarities and differences between classes and structures:

Classes and structures have the following functions:

Define storage properties

Define method

Define subscript

Define the constructor

Define extension

Implementation Agreement

 

Functions only available for classes:

Can inherit another class

Ability to check the type of runtime objects

Destructor object releases resources

Reference counting allows multiple references to an instance

 

Selection principle:

    The structure is a value type, and each instance has no unique identifier. The following two array instances are essentially the same, and they can be replaced with each other.

var studentList1: [String] = ["张三", "李四", "王 五"]
var studentList2: [String] = ["Zhang San", "Li Si", "Wang Wu"]


    But when we mention the class, it is a reference type, and each instance has a unique identifier. Let's look at the following employee Employee class code:

class Employee {
    var no = 0
    var name = ""
    var job = ""
    var salary = 0.0
}
 
var emp1 = Employee ()
emp1.no = 100
emp1.name = "Tom"
emp1.job = "SALES"
emp1.salary = 9000
 
var emp2 = Employee ()
emp2.no = 100
emp2.name = "Tom"
emp2.job = "SALES"
emp2.salary = 9000


    Even if the contents of the two employee instances of emp1 and emp2 are exactly the same, this does not mean that they are the same employee, but they are similar. Each employee instance has a unique logo behind it.

Let's take another look at the Department Structure.

struct Department {
    var no: Int = 0
    var name: String = ""
}
 
var dept1 = Department ()
dept1.no = 20
dept1.name = "Research"
 
var dept2 = Department ()
dept2.no = 20
dept2.name = "Research"


    Why is Department designed as a structure rather than a class? It depends on our understanding of two different departments. If we have the same department number (no) and department name (name), we think they are the same Department, then you can define Department as a structure, which is different from Employee employees.

 

Welcome to follow Guandongsheng on Sina Weibo @tony_ 关 东升.
Follow the Zhijie classroom public platform for the latest technical articles, books, and tutorial information
                              650) this.width = 650; "title =" 00.png "alt =" wKioL1bWSd6gJs1rAAAs2MBEZnc134.png "src =" http://s5.51cto.com/wyfs02/M00/7C/AE/wKioL1bWSd6gJs1rAAAs2MBEZnc134.png "/>
For more excellent iOS, Cocos, and mobile design courses, please pay attention to the official website of Zhijie Classroom: http://www.zhijieketang.com
Zhijie Classroom Forum website: http://51work6.com/forum.php

 

 



"Learning Swift from scratch" study notes (Day 30)-choose class or structure?

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.