迅捷cad
In this tutorial, we’ll be discussing an important building block in Swift, namely Structures. Swift struct keyword is used to create structures in swift programming.
在本教程中,我们将讨论Swift中的一个重要构建模块,即Structures 。 Swift struct关键字用于在Swift编程中创建结构。
迅捷结构 (Swift Struct)
On a high level, Structures are custom data types that let us store data in the form of properties and functions. One of the highlights of Structures is that they are value types and not reference types.
Let’s look at the basic syntax of a struct
.
从高层次上讲,结构是自定义数据类型,可让我们以属性和函数的形式存储数据。 结构的亮点之一是它们是值类型而不是引用类型。
让我们看一下struct
的基本语法。
struct Name {
// properties here
}
A Structure is defined using the keyword struct
followed by the name and curly braces.
使用关键字struct
定义struct
后跟名称和花括号。
Properties are normal variables/constants that are defined in a struct
. We’ll look more into Properties in a later tutorial.
属性是在struct
中定义的普通变量/常量。 我们将在以后的教程中深入研究“属性”。
Unlike other languages, Struct in Swift is fully featured. They allow properties, functions, initializers, protocols etc. Unlike Classes, Structs in Swift doesn’t support inheritance.
与其他语言不同,Swift中的Struct具有完整功能。 它们允许属性,函数,初始化器,协议等。与类不同,Swift中的结构不支持继承。
Let’s open up the playground and play around with structures in it.
让我们打开操场并在其中玩耍。
Swift结构示例 (Swift Struct Example)
Here’s an example of a Struct definition with properties.
这是带有属性的Struct定义的示例。
struct Rectangle {
var width = 0
var height = 0
}
An instance of the above struct
can be created in the following way.
可以通过以下方式创建上述struct
的实例。
var rectangle = Rectangle()
To access or modify the properties of a structure we use the dot operator on the instance as shown below.
要访问或修改结构的属性,我们在实例上使用点运算符,如下所示。
print(rectangle.width) // prints 0
print(rectangle.height) //prints 0
rectangle.width = 10
rectangle.height = 10
If the instance was initialized as let
, changing the properties won’t be possible.
如果实例初始化为let
,则无法更改属性。
let rectangle = Rectangle()
rectangle.width = 10 //compile-time error
rectangle.height = 10 //compile-time error
Defining functions inside a Structure
在结构内部定义功能
struct Rectangle {
var width = 0
var height = 0
func area(width :Int, height: Int) -> String
{
return "Area is \(width*height)"
}
}
Calling the function using an instance of the structure as shown below:
使用结构实例调用函数,如下所示:
var rectangle = Rectangle()
rectangle.width = 10
rectangle.height = 10
rectangle.area(width: rectangle.width, height: rectangle.height) //prints 100
在函数内部修改结构属性 (Modifying struct properties inside functions)
To modify a property inside a function we need to mark the function with the keyword mutating.
要在函数内部修改属性,我们需要使用关键字mutating标记该函数。
struct Rectangle {
var width = 0
var height = 0
mutating func printModifiedWidth() -> String
{
width = width*2
return "Modified width is \(width)"
}
}
var rectangle = Rectangle()
rectangle.width = 10
rectangle.printModifiedWidth() //prints "Modified width is 20"
Note: Properties inside a structure can’t be changed inside a simple function.
Declaring function as mutating inside struct
allows us to alter properties in Structures.
注意 :结构内的属性无法在简单函数内更改。
声明功能内变异 struct
使我们能够在结构ALTER性能。
Swift结构初始化器 (Swift Struct Initialisers)
Default initializer takes 2 forms. Either an empty initializer() or the memberwise initializer that lists the structure’s properties inside its parenthesis so that you can assign new values to them.
默认的初始化程序有2种形式。 一个空的 initializer()或在该结构的圆括号内列出该结构的属性的成员初始化程序,以便可以为其分配新值。
A memberwise initializer for the above structure would look as follows:
上述结构的成员初始化器如下所示:
var rectangle = Rectangle(width: 10, height: 10)
A custom initialiser can be defined as follows:
可以将自定义初始化程序定义如下:
struct Rectangle {
var width = 0
var height = 0
init(width: Int, height: Int) {
self.width = width*2
self.height = height*2
}
}
var rectangle = Rectangle(width: 10, height: 10)
print(rectangle.width) //prints 20
print(rectangle.height) //prints 20
Initialisers Delegation is also possible as shown below.
如下所示,也可以进行初始化程序委托 。
struct Rectangle {
var width = 0
var height = 0
init(width: Int, height: Int) {
self.width = width
self.height = height
}
init(width: Int) {
self.init(width: width, height: width)
}
}
var square = Rectangle(width: 10)
print(square.width) //prints 10
print(square.height) //prints 10
Swift结构静态函数 (Swift Struct Static Functions)
Static Functions can be called without creating an instance of Structure as shown below:
可以在不创建Structure实例的情况下调用静态函数,如下所示:
struct Rectangle {
var width = 0
var height = 0
static func printString() -> String
{
return "Hello how you're doing"
}
}
Rectangle.printString() //prints "Hello how you're doing"
Swift Struct计算属性 (Swift Struct Computed Properties)
Properties can be computed dynamically using the following syntax.
可以使用以下语法动态计算属性。
struct Rectangle {
var width = 0
var height = 0
var area: Int {
get {
return width*height
}
set {
area = 0
}
}
}
set
is used to give an initial value to the property.
get
is the one that actually returns the computed property.
Using the computed properties the area
property would be automatically calculated when the width
or height
is altered.
set
用于为属性赋予初始值。
get
是实际返回计算属性的对象。
使用计算出的属性,当width
或height
更改时,将自动计算area
属性。
var rectangle = Rectangle()
print(rectangle.area) //prints 0
rectangle.width = 20
rectangle.height = 20
print(rectangle.area) //prints 400
Swift结构是通过值而不是通过引用传递的 (Swift Structures are passed by values and not by reference)
struct Rectangle {
var width = 0
var height = 0
var area: Int {
get {
return width*height
}
set {
area = 0
}
}
}
var rectangle = Rectangle()
print(rectangle.area)
rectangle.width = 20
rectangle.height = 20
var rect = rectangle
rect.width = 30
print(rectangle.width) //prints 20 and NOT 30
This brings an end to swift struct tutorial. We’ll be discussing Classes in our next Swift Tutorial.
这样就结束了快速的结构教程。 我们将在下一个Swift教程中讨论类。
Reference: Official Documentation
参考: 官方文档
迅捷cad