迅捷cad_迅捷属性

迅捷cad

In this tutorial, we’ll be discussing about Swift Properties. Properties are an important part of Classes, Structures and Enumerations in Swift.

在本教程中,我们将讨论Swift属性。 属性是Swift中ClassStructuresEnumerations的重要组成部分。

迅捷属性 (Swift Properties)

Swift Properties are broadly classified into two types.

Swift属性大致分为两种类型。

  1. Stored Properties: store constants and variables and are provided by classes and structures

    存储的属性 :存储常量和变量,由类和结构提供
  2. Computed Properties: Instead of storing values, these calculate values. Are provided by classes structs as well as enumerations.

    计算属性 :计算值而不是存储值。 由类结构和枚举提供。

Note: A computed property can’t be a constant.

注意 :计算属性不能为常数。

Swift存储属性示例 (Swift Stored Property Example)

An example of Stored Properties is given below.

下面给出了一个存储属性的例子。

struct Rect{
    
    var length : Int
    let breadth : Int
    
}

var r = Rect(length: 5, breadth: 5)
r.length = 6

Both the length and breadth are stored properties (variable and constant respectively) in the above snippet.

lengthbreadth都是上述代码段中存储的属性(分别为变量和常数)。

Modifying a Constant Structure instance

修改常量结构实例

let r1 = Rect(length: 5, breadth: 5)
r1.length = 6 //compile-time error.

If the struct is intialised as a constant, changing stored properties isn’t possible even though they’re declared as variables. The reason is that structs are value types. The same isn’t the case with classes since they’re reference types.

如果将结构初始化为常量,则即使将其声明为变量,也无法更改存储的属性。 原因是结构是值类型 。 对于类,情况并非如此,因为它们是引用类型

Swift Lazy属性 (Swift Lazy Properties)

As per Apple’s documentation Swift Lazy property is defined as:

根据Apple的文档,Swift Lazy属性定义为:

Unlike other properties, a lazy property is initialised right before it’s being accessed for the first time.

与其他属性不同,惰性属性是在首次访问之前初始化的。

  1. Lazy properties are useful when preventing unnecessary object creation and subsequently saving memory.

    当防止不必要的对象创建并随后节省内存时,惰性属性很有用。
  2. Typically a property is defined as lazy when its dependent on other properties that are not known yet.

    通常,当某个属性依赖于其他未知属性时,它被定义为惰性。
  3. A lazy modifier cannot be added to a constant. Constants require an initial value before the initialisation completes which is not the case with lazy properties.

    惰性修饰符不能添加到常量。 常数在初始化完成之前需要一个初始值,而懒惰属性则并非如此。
  4. A lazy property must have an initialiser and cannot be used simply with any variable.

    惰性属性必须具有初始化程序,不能简单地与任何变量一起使用。

The following is a wrong usage case of lazy var.

以下是lazy var的错误用法情况。

struct Rect{
    
    lazy var length : Int //compile-time error. lazy properties require an initaliser.
    let breadth : Int
    
}

An example of lazy var is given below:

下面是lazy var的示例:

truct Rect{
    
    var length : Int
    let breadth : Int
    
    init(length : Int, breadth: Int) {
        print("Rect struct is initialised now from the lazy var property")
        self.length = length
        self.breadth = breadth
    }
    
}

struct Square{
    
    var sidesEqual : Bool
    
    lazy var r = Rect(length: 6, breadth: 6)
    
    init(sidesEqual : Bool) {
        self.sidesEqual = sidesEqual
    }
}

var s = Square(sidesEqual: false)

if s.sidesEqual
{
    print(s.r.length)
}
else{
    print("Rect struct hasn't been initialised using the lazy var")  //this gets printed
}

var s1 = Square(sidesEqual: true)

if s1.sidesEqual
{
    print(s1.r.length) //prints Rect struct is initialised now from the lazy var property \n 6
}
else{
    print("Rect struct hasn't been initialised using the lazy var") //not printed
}

It’s evident in the above code that the Rect struct is instantiated from the lazy var instance only when it meets the condition.

从上面的代码中可以明显看出,只有在满足条件时才从惰性var实例实例化Rect结构。

Lazy Properties are handy when your code has many object instances since it’ll create the declared instances only when they’re needed.

当代码中有许多对象实例时,惰性属性非常方便,因为只有在需要时才创建声明的实例。

Once a lazy property is initialised, for further accesses it generally reuses the first instance.

一旦初始化了惰性属性,为了进行进一步的访问,它通常会重用第一个实例。

Note: If a property marked with the lazy modifier is accessed by multiple threads simultaneously and the property has not yet been initialized, there is no guarantee that the property will be initialized only once.

注意 :如果标记有lazy修饰符的属性同时被多个线程访问,并且该属性尚未初始化,则不能保证该属性仅被初始化一次。

带关闭的Swift Lazy属性 (Swift Lazy Properties with a Closure)

class Name {
    
    var name : String?
    lazy var greet : String = {[weak self] in
        
        guard let s = self else { return "Unable to unwrap self"}
        guard let n = s.name else { return "No name found" }
        
        return "Hi, \(n)"
       
    }()
    
    init(name: String) {
        self.name = name
    }
    
}

var n = Name(name: "Anupam")
print(n.greet) //prints "Hi, Anupam\n"
重要事项 (Important Points)
  1. We’ve defined a closure inside the lazy var property in the above code.

    在上面的代码中,我们在lazy var属性内定义了一个闭包。
  2. The Closure returns a String.

    闭包返回一个字符串。
  3. To eliminate a strong reference cycle, we’ve captured a weak self.

    为了消除强参考周期,我们捕获了一个weak self
  4. guard let is used to for optional unwrapping.

    guard let用于进行可选的展开。

Let’s do a few modifications of the above code and see how it behaves.

让我们对上面的代码做一些修改,看看它的行为。

var n = Name(name: "Anupam")
print(n.greet) //prints "Hi, Anupam"
n.name = nil
print(n.greet) //prints "Hi, Anupam"

The above snippet is an interesting case that shows that lazy var property is re-used everytime. Changes to the name property does nothing.

上面的代码片段是一个有趣的案例,它表明lazy var属性每次都被重用。 更改name属性不会执行任何操作。

var n = Name(name: "Anupam")
n.name = nil
print(n.greet) //prints "No name found"

In the above snippet, the second guard let statement fails to unwrap the optional string.

在以上代码段中,第二个保护let语句未能解开可选字符串。

Swift计算属性 (Swift Computed Properties)

Unlike Stored, Computed Properties don’t store values. Instead, they’re used as getters and optional setters to retrieve and set other properties and values indirectly.
A basic example is given below.

与“存储”不同,“计算属性”不存储值。 而是将它们用作获取器和可选的设置器,以间接检索和设置其他属性和值。
下面给出一个基本示例。

struct Rect{
    
    var length : Double
    let breadth : Double
    
    var area : Double {
        
        get{
            return length*breadth
        }
        set(newArea)
        {
            length = newArea/breadth
        }
    }
}

var r = Rect(length: 6, breadth: 5)
print(r.area) //prints 30.0
r.area = 40
print(r.length) //prints 8.0

In the above code getters and setters are used as get { } and set(param_name){ } on the computed property area. The getters and setters are accessed using the dot syntax.

在上面的代码中,getter和setter用作计算属性area上的get { }set(param_name){ } 。 使用点语法访问getter和setter。

If a param name isn’t specified inside the setter, Swift assigns the default name as newValue.

如果未在setter中指定参数名称,则Swift会将默认名称分配为newValue

struct Rect{
    
    var length : Double
    let breadth : Double
    
    var area : Double {
        
        get{
            return length*breadth
        }
        set
        {
            length = newValue/breadth
        }
    }
}

Computed Properties can’t be assign as a lazy var property. Computed Properties that have a get and set defined can’t be set as a constant let.

不能将计算属性分配为惰性var属性。 定义了get和set的计算属性不能设置为常量let

只读计算属性 (Read-only Computed Properties)

A Computed property without a setter is a read-only computed property. They can be defined as a constant.

不带setter的Computed属性是只读的计算属性。 它们可以定义为常量。

struct Rect{
    
    var length : Double
    let breadth : Double
    
    var area : Double {
        get{
            return length*breadth
        }
    }
}

var r = Rect(length: 6, breadth: 5)
r.area = 50 //compile-time error. area is a get-only property.

We can let go of the get keyword in the above case too:

在上述情况下,我们也可以放开get关键字:

var area : Double {
            return length*breadth
    }

迅捷物业观察员 (Swift Property Observers)

Swift Property Observers respond to changes in the property value. These are typically used when two property values are dependent on each other. They contain two methods:

Swift属性观察者响应属性值的更改。 这些属性通常在两个属性值相互依赖时使用。 它们包含两种方法:

  1. willSet : This gets triggered just before the value is stored. It allows us to read the old value before its changed. We can access the new value using the keyword newValue

    willSet :这将在存储值之前触发。 它允许我们在更改之前读取旧值。 我们可以使用关键字newValue访问新值
  2. didSet : This gets triggered after the value is stored. It lets us read both the old and new values. We can access the old value using the keyword oldValue

    didSet :在值存储后触发。 它使我们可以读取旧值和新值。 我们可以使用关键字oldValue访问旧值

Property Observers get triggered every time the value is set. Let’s use Property Observers in an example where we need to convert yards to inches.

每次设置该值都会触发属性观察器。 让我们在需要将码转换为英寸的示例中使用属性观察器。

struct yardToInchesConversion{
    
    var yard : Double = 0 {
        willSet{
            print("new value of yards \(newValue)")
        }
        didSet{
            print("old value of yards \(oldValue)")
            inches = yard*36
            print("Updated value of inches \(inches)")
        }
    }
    var inches : Double = 0
}

var yi = yardToInchesConversion()
yi.yard = 22

//The Following gets printed on the console:
new value of yards 22.0
old value of yards 0.0
Updated value of inches 792.0

Swift的全球和本地属性 (Swift Global and Local Properties)

Global variables are variables that are defined outside of any function, method, closure, or type context. Local variables are variables that are defined within a function, method, or closure context.
Global constants and variables are always computed lazily without the need to be marked with a lazy modifier.

全局变量是在任何函数,方法,闭包或类型上下文之外定义的变量。 局部变量是在函数,方法或闭包上下文中定义的变量。
全局常量和变量始终是惰性计算的,不需要使用惰性修饰符进行标记。

雨燕类型属性 (Swift Type Properties)

  • Type Properties are used on the type(class/struct/enum) instead of the instance of that type.

    类型属性用于类型(类/结构/枚举),而不是该类型的实例。
  • Type Properties are defined with the keyword static.

    类型属性是用关键字static定义的。
  • Static Type Properties can’t be overridden in the subclass. The keyword class is used on the computed properties in this case.

    静态类型属性不能在子类中覆盖。 在这种情况下,将关键字class用于计算的属性。
  • class keyword isn’t supported with stored properties.

    存储的属性不支持class关键字。

The following code snippets demonstrate the above concepts clearly.

以下代码段清楚地说明了上述概念。

class A {
    
    static var i : Int = 5
    static var name : String {
        return "Hello World"
    }

    class var multiplyByANumber : Int {
        return i*5
    }
    
    //class var j : Int = 1   //Not supported. Won't compile.
    static func printI()
    {
        print("Value of i is \(i)")
    }
    class func appendClassName()
    {
        print("Class A Hello World")
    }
    
}

class SubClass : A {
    
    //static var i = 10    Won't Compile
    override class var multiplyByANumber : Int{
        return i*5*5
    }
    
    override class func appendClassName(){
        print("Class SubClass Hello World")
    }
    
}

Output of above program:

以上程序的输出:

Class A Hello World
25
Class SubClass Hello World
125

迅捷下标 (Swift Subscripts)

Swift Subscripts are shortcuts for accessing the member elements of a collection, list, or sequence. To access an element from an array, dictionary or a list we use the form array[index], dictionary[key] etc. Similarly, we can define a subscript for any type.

Swift下标是访问集合,列表或序列的成员元素的快捷方式。 要访问数组,字典或列表中的元素,我们使用array [index],dictionary [key]等形式。类似地,我们可以为任何类型定义下标。

We can define multiple subscripts for the same type and the appropriate subscript overload to use is selected based on the type of index value you pass to the subscript.

我们可以为同一类型定义多个下标,并根据传递给下标的索引值的类型来选择要使用的适当下标重载。

The syntax for a subscript is similar to computed properties as shown below.

下标的语法类似于如下所示的计算属性。

subscript(index: Int) -> Int {
    get {
        // return an appropriate subscript value here
    }
    set(newValue) {
        // perform a suitable setting action here
    }
}

An example implementation of subscripts is given below.

下标的示例实现在下面给出。

class M {
    private var month = ["Jan", "Feb", "March", "April"]
    subscript(index: Int) -> String {
        get {
            return month[index]
        }
        set(newValue) {
            self.month[index] = newValue
        }
    }
}
var m = M()
m[3] //April 
m[0] = "Dec"

This brings an end to Swift properties tutorial.

这样就结束了Swift属性教程。

Reference: Apple Docs

参考: Apple Docs

翻译自: https://www.journaldev.com/16806/swift-properties

迅捷cad

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值