IOS: learning notes, Swift through Boolean (translated from: https://developer.apple.com/swift/blog/ Aug 5, 2014 Boolean)
View Swift through Boolean
A simple Bool type contains many major Swift functions. It is an interesting demonstration of how to build a simple type. this article will create a new MyBool type that is very similar to the Bool type in design and implementation. we hope that by designing and implementing a simple Swift type, you can better understand how the Swift language works.
enum MyBool { case myTrue, myFalse}
Let's start with the basic definition. The MyBool type has two different states, which are implemented using enum.
extension MyBool { init() { self = .myFalse }}
To avoid misunderstanding, we name the States myTrue and myFalse. We want MyBool () to be set to false by default, So we implement an init method:
The Swift enum Declaration implies the valid range of the enumerated value, which allows us to use MyBool. myFalse can be used even when the environment type is allowed. myFalse. however, we need to assign values for our types using constants true and false. to implement this, let MyBool implement the BooleanLiteralConvertible protocol:
Extension MyBool: BooleanLiteralConvertible {
Extension MyBool: BooleanLiteralConvertible {static func convertFromBooleanLiteral (value: Bool)-> MyBool {return value? MyTrue: myFalse} // you can assign 'true' and 'false' to MyBoolvar a: MyBool = true.
After this step is completed, we have the basic type, but we have not done enough. booleans must be used to determine if conditions. swift uses the BooleanType Protocol to implement it. It allows any type to be used for logical conditions:
Extension MyBool: BooleanType {var boolValue: Bool {switch self {case. myTrue: return true case. myFalse: return false }}// now MyBool can be used for 'if' and 'while 'condition judgment. if {}
We also hope that variables that comply with the BooleanType protocol can be converted to MyBool, So we add
Extension MyBool {// MyBool you can use BooleanType to construct init (_ v: BooleanType) {if v. boolValue {self =. myTrue} else {self =. myFalse }}// the boolean-like type can be set now. var basicBool: Bool = truea = MyBool (basicBool)
Note: Use _ in the initialization parameter to disable the parameter keyword. It allows the use of MyBool (x) syntax to replace the default MyBool (v: x ).
Now we have basic functions. Below we start to define some operators from =. the compiler does not enable simple enumeration to automatically Equatable, so additional code is required. here, you can implement the Equatable protocol and the = Operator to achieve equal comparison for any type. if MyBool does not have Equatable, You can implement the following:
Extension MyBool: Equatable {} func = (lhs: MyBool, rhs: MyBool)-> Bool {switch (lhs, rhs) {case (. myTrue ,. myTrue ),(. myFalse ,. myFalse): return true default: return false }}// you can use ==and! = Compare if a = a {} if! = {}
Here we use some simple pattern matching in the switch statement. Since MyBool has been Equatable, we don't need to implement it any more! = Operator. Here we add a binary operation:
func &(lhs: MyBool, rhs: MyBool) -> MyBool { if lhs { return rhs } return false}func |(lhs: MyBool, rhs: MyBool) -> MyBool { if lhs { return true } return rhs}func ^(lhs: MyBool, rhs: MyBool) -> MyBool { return MyBool(lhs != rhs)}
Using basic operators, we can implement other useful single-element and combined value assignment operators, such:
Prefix func! (A: MyBool)-> MyBool {return a ^ true} // The combination value is func & = (inout lhs: MyBool, rhs: MyBool) {lhs = lhs & rhs}
In the & = Operator, because the left value needs to be modified, we add the inout modifier to the parameter. for value types (such as enum and struct), Swift provides you with complete modification operation control.
Through these, simple MyBool types have all the basic operations and operators. We hope the prompts in this article can provide reference for defining advanced types.
From: Apple official blog 2014-8-5 Boolean
Postscript:
Although the sparrow is small, Swift makes our code more beautiful through protocol and operator definition.