Properties

作者: 宋奕Ekis | 来源:发表于2021-08-09 15:30 被阅读0次

Computed properties are provided by classes, structures, and enumerations.

Stored properties are provided only by classes and structures.

Stored Properties

Stored Properties of Constant Structure Instances

struct FixedLengthRange {
    var firstValue: Int
    let length: Int
}
let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)
// this range represents integer values 0, 1, 2, and 3
rangeOfFourItems.firstValue = 6
// this will report an error, even though firstValue is a variable property

If the final type of an instance of Structure is constant, just like the example above, we cannot change the inner property even if it’s variable.

Due to the structure is value types.

So for Class which is refrence types, it is not same, we can change the inner value even if it is assign a constant value.

Lazy Stored Properties

We all know how to create a lazy property, but I I will highlight you this at here:

==If a property marked with the lazy modifier is accessed by multiple threads simultaneously and the property hasn’t yet been initialized, there’s no guarantee that the property will be initialized only once.==

Property Wrappers

It is the charecataristic I never heard, so it is amazing for me.

@propertyWrapper
struct TwelveOrLess {
    private var number = 0
    var wrappedValue: Int {
        get { return number }
        set { number = min(newValue, 12) }
    }
}

We can use the @propertyWrapper keyword to define a Struct and use wrappedValue to make a rule, just like the example above.

Then,

struct SmallRectangle {
    @TwelveOrLess var height: Int
    @TwelveOrLess var width: Int
}

var rectangle = SmallRectangle()
print(rectangle.height)
// Prints "0"

rectangle.height = 10
print(rectangle.height)
// Prints "10"

rectangle.height = 24
print(rectangle.height)
// Prints "12"

we can use this struct to define a property, and this property has to comply this rule, just like the example above.

The height and width cannot beyond 12.

Setting Initial Values for Wrapped Properties

@propertyWrapper
struct SmallNumber {
    private var maximum: Int
    private var number: Int

    var wrappedValue: Int {
        get { return number }
        set { number = min(newValue, maximum) }
    }

    init() {
        maximum = 12
        number = 0
    }
    init(wrappedValue: Int) {
        maximum = 12
        number = min(wrappedValue, maximum)
    }
    init(wrappedValue: Int, maximum: Int) {
        self.maximum = maximum
        number = min(wrappedValue, maximum)
    }
}

We can see the example above, there are 3 initializer in SmallNumber, and we can make the default value on each one.

and use it like below:

struct ZeroRectangle {
    @SmallNumber var height: Int
    @SmallNumber var width: Int
}

var zeroRectangle = ZeroRectangle()
print(zeroRectangle.height, zeroRectangle.width)
// Prints "0 0"
struct UnitRectangle {
    @SmallNumber var height: Int = 1
    @SmallNumber var width: Int = 1
}

var unitRectangle = UnitRectangle()
print(unitRectangle.height, unitRectangle.width)
struct NarrowRectangle {
    @SmallNumber(wrappedValue: 2, maximum: 5) var height: Int
    @SmallNumber(wrappedValue: 3, maximum: 4) var width: Int
}

var narrowRectangle = NarrowRectangle()
print(narrowRectangle.height, narrowRectangle.width)
// Prints "2 3"

narrowRectangle.height = 100
narrowRectangle.width = 100
print(narrowRectangle.height, narrowRectangle.width)
// Prints "5 4"
struct MixedRectangle {
    @SmallNumber var height: Int = 1
    @SmallNumber(maximum: 9) var width: Int = 2
}

var mixedRectangle = MixedRectangle()
print(mixedRectangle.height)
// Prints "1"

mixedRectangle.height = 20
print(mixedRectangle.height)
// Prints "12"

Especially the last example above, we can just fill apartful values of initializer which can aim autoly.

Projecting a Value From a Property Wrapper

In addition to wrappedValue, projectedValue which can be any type to store a explicit value assioated others is another keyword in Property Wrapper.

And we can access the ProjectedValue via $.

@propertyWrapper
struct SmallNumber {
    private var number = 0
    var projectedValue = false
    var wrappedValue: Int {
        get { return number }
        set {
            if newValue > 12 {
                number = 12
                projectedValue = true
            } else {
                number = newValue
                projectedValue = false
            }
        }
    }
}
struct SomeStructure {
    @SmallNumber var someNumber: Int
}
var someStructure = SomeStructure()

someStructure.someNumber = 4
print(someStructure.$someNumber)
// Prints "false"

someStructure.someNumber = 55
print(someStructure.$someNumber)
// Prints "true"

Let’s think!

相关文章

  • Java | Properties类操作

    一、Java Properties类 Properties 类(Java.util.Properties)用于读取...

  • MyBatis——全局配置文件

    1.properties 可以使用properties来引入外部properties配置。 resource:引入...

  • 《Oracle Java SE编程指南》26-06:动态创建任何

    内容导航: 前言 1、Properties文件 2、反射 1、Properties文件 使用.properties...

  • Spring Boot 配置项加密

    Spring Boot内置的properties支持为我们读取properties带来了便利。Properties...

  • Properties:

    Properties类表示了一个持久的属性集,Properties 可以保存在流中或从流中加载.属性列表中每个键及...

  • Properties

    这个类表示一个属性集,继承于HashTable,内部是String键值对。可在流中加载或保存。字符输入/输出流用的...

  • properties

    读取配置文件

  • Properties

    属性分为类型属性和实例属性。使用static和class关键字修饰的属性为类型属性,否则为实例属性。其中stati...

  • Properties

    Properties是hashtable的子类。也就是说,它具备map集合的特点。而且它里面存储的键值对都是字符串...

  • Properties

    nfvo RabbitMQ nfvo.rabbit.brokerIp = localhostnfvo.rabbit...

网友评论

    本文标题:Properties

    本文链接:https://www.haomeiwen.com/subject/nimzvltx.html