如何在swift中自定义基本类型Bool
来源:互联网 发布:spark高级数据分析pdf 编辑:程序博客网 时间:2024/04/30 09:13
Boolean
The boolean Bool type in Swift underlies a lot of primitive functionality, making it an interesting demonstration of how to build a simple type. This post walks through the creation of a new MyBool type designed and implemented to be very similar to the Bool type built into Swift. We hope this walk through the design of a simple Swift type will help you better understand how the language works.
Let’s start with the basic definition. The MyBool type models two different cases, perfect for an enum:
enum MyBool {case myTrue, myFalse}
To reduce confusion in this post, we’ve named the cases myTrue and myFalse. We want MyBool() to produce a false value, and can do so by providing an init method:
extension MyBool {init() { self = .myFalse }}
Swift enum declarations implicitly scope their enumerators within their body, allowing us to refer toMyBool.myFalse and even .myFalse when a contextual type is available. However, we want our type to work with the primitive true and false literal keywords. To make this work, we can make MyBoolconform to the BooleanLiteralConvertible protocol like this:
extension MyBool : BooleanLiteralConvertible {static func convertFromBooleanLiteral(value: Bool) -> MyBool {return value ? myTrue : myFalse}}// We can now assign 'true' and 'false' to MyBool.var a : MyBool = true
With this set up, we have our basic type, but we still can’t do much with it. Booleans need to be testable within an if condition. Swift models this with the BooleanType protocol, which allows any type to be used as a logical condition:
extension MyBool : BooleanType {var boolValue: Bool {switch self {case .myTrue: return truecase .myFalse: return false}}}// Can now test MyBool in 'if' and 'while' statement conditions.if a {}
We also want anything that conforms to BooleanType to be castable to MyBool, so we add:
extension MyBool {// MyBool can be constructed from BooleanTypeinit(_ v : BooleanType) {if v.boolValue {self = .myTrue} else {self = .myFalse}}}// Can now convert from other boolean-like types.var basicBool : Bool = truea = MyBool(basicBool)
Note that the use of _ in the initializer argument list disables the keyword argument, which allows theMyBool(x) syntax to be used instead of requiring MyBool(v: x).
Now that we have basic functionality, let’s define some operators to work with it, starting with the ==operator. Simple enums that have no associated data (like MyBool) are automatically made Equatableby the compiler, so no additional code is required. However, you can make arbitrary types equatable by conforming to the Equatable protocol and implementing the == operator. If MyBool weren’t alreadyEquatable, this would look like this:
extension MyBool : Equatable {}func ==(lhs: MyBool, rhs: MyBool) -> Bool {switch (lhs, rhs) {case (.myTrue,.myTrue), (.myFalse,.myFalse):return truedefault:return false}}// Can now compare with == and !=if a == a {}if a != a {}
Here we’re using some simple pattern matching in the switch statement to handle this. Since MyBool is now Equatable, we get a free implementation of the != operator. Lets add binary operations:
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)}
With the basic operators in place, we can implement a variety of helpful unary and compound assignment operators as well, for example:
prefix func !(a: MyBool) -> MyBool {return a ^ true}// Compound assignment (with bitwise and)func &=(inout lhs: MyBool, rhs: MyBool) {lhs = lhs & rhs}
The &= operator takes the left operand as inout because it reads and writes to it, and the effect must be visible to the user of the operator. Swift gives you complete control over mutability of operations on value types like enum and struct.
With this, the simple MyBool type has all of the basic operations and operators. Hopefully this post gives you a few tips that you can apply to your own code when defining higher-level types.
转载自Apple Swift Blog:https://developer.apple.com/swift/blog/
- 如何在swift中自定义基本类型Bool
- swift(五) Bool类型
- 如何在Swift中创建自定义控件
- Swift 3.0中C语言指针类型在Swift编程语言中如何操作
- C语言自定义bool类型
- Swift学习之五:Bool类型
- Swift学习之五:Bool类型
- Swift学习之五:Bool类型
- 四 Swift 3.0之 Bool类型
- QT中如何在ComboBox中设置存放自定义类型
- Swift中enum自定义类型的实现
- 如何在WordPress 3.0中使用自定义内容类型
- 如何在nsdictionary/nsmutabledictionary中使用自定义类型的键
- Python中bool类型转换
- Swift Bool
- 自定义bool类型转换操作符
- Swift基本类型
- Swift教程-基本类型
- 小说明
- 《数据结构》第一章知识导图
- 直击用户大脑——用户研究新方法(眼动与脑电数据分析)
- delete后仍然可以调用问题
- ajax简介
- 如何在swift中自定义基本类型Bool
- Cocos2d-x中精灵图片的渲染顺序
- hdu-oj 3351 Seinfeld
- 小贝学习CSS-字体
- UVA1492 - Adding New Machine(扫描线)
- Windows下修改Git bash的HOME路径
- 杭电3351 Seinfeld(贪心)
- Subsets I, II
- 【LeetCode】Minimum Path Sum 解题报告