1.

Take into consideration the following struct:

Answer» PUBLIC struct Temperature{ public var temp: DOUBLE public init(temp: Double) { self.temp = temp }}

Can we do an initialization using the following code:

var temp: Temperature = 60.5

Yes, Swift SPECIFIES protocols that allow you to use the assignment operator to initialize a type with literal values. Literal initialization of a certain type is possible by adopting the corresponding protocol and supplying a public initializer. You implement ExpressibleByFloatLiteral as follows in the example of Temperature:

extension Temperature: ExpressibleByFloatLiteral { public init(floatLiteral VALUE: FloatLiteralType) { self.init(temp: value) }}

After this, we can do the above-given initialization without any errors.



Discussion

No Comment Found