Description of a custom type for Swift type creation
This article describes how to create a custom Swift type, this article describes the User-Defined prototype, implementation of default values, support for basic Boolean initialization, support for Bool type determination, support for compatibility with various types, and improvement of the OCBool Boolean gene system, for more information, see
Dear friends, the Bool type in Swift has very important Syntax Functions and supports the logic judgment system in the entire Swift system. After studying and learning the old code, the Bool type is actually an encapsulation of the basic Boolean Type. Friends may stick their fingers to ask the old code about the Bool type and the Boolean type. The difference is that, the former is an enumeration-based combination type, while the latter is a basic type. There are only two true and false types.
#### Custom prototype
Next, the old Code creates an OCBool type based on the Bool idea to let our friends know how Swift is playing.
Let's take a look at the definition of OCBool.
##### Sample code:
The Code is as follows:
Enum OCBool {
Case ocTrue
Case ocFalse
}
##### Note:
Lines 2nd and 3rd in the code can be merged into one line for writing, as written in Apple's official Blog.
In the code, you must note that OCBool is a type name, so the first letter must be in upper case, while ocTrue and ocFalse in case must be in lower case.
#### Default implementation
Line, we give a pretty definition, but according to the experience of traditional languages, the Bool value is false by default, so we should do the same for OCBool, we use the type Extension Technology to add this default feature:
The Code is as follows:
Extension OCBool {
Init (){
Self =. ocFalse
}
}
##### Note:
● Line 4 in the Code: the extension keyword is very powerful. You can use this to create many interesting things. I suggest you go to Github to see a project named "Swiftz, it will be extended to the extreme.
● Line 1 in the Code: self =. the ocFalse syntax is very confusing to beginners. Why is there a strange syntax? Because Daniel Chris added the smart inference function for types in Swift. In the apple Blog, the "Context" concept is mentioned, because this line of statements is in the enumeration of OCBool, and its Context is the definition body of OCBool. Of course, the compiler knows. ocFalse is OCBool. ocFalse, so the syntax here is very neat.
Now we can use this Bool type using the following method.
##### Sample code:
Copy the Code as follows:
Var result: OCBool = OCBool ()
Var result1: OCBool =. ocTrue
#### Support for basic Boolean Initialization
As described in the Code above, we can only assign values by type or enumeration items. This is a combination type usage, but in coding days, we always want to deal with true or false directly, that is, we hope to do this,
The sample code is as follows:
The Code is as follows:
Var isSuccess: OCBool = true
If you use this method directly, the following error occurs:
The Code is as follows:
/Users/tyrion-OldCoder/Documents/Learning/BoolType/main. swift: 30: 24: Type 'ocbool 'does not conform to protocol 'booleanliteralconvertible'
The reason the compiler is roaring is that our type does not comply with the "Boolean literal conversion protocol". Next we will fix this problem,
##### Sample code:
The Code is as follows:
Import Foundation
Println ("Hello, World! ")
Enum OCBool {
Case ocTrue
Case ocFalse
}
Extension OCBool: BooleanLiteralConvertible {
Static func convertFromBooleanLiteral (value: Bool)-> OCBool {
Return value? OcTrue: ocFalse
}
}
Var isSuccess: OCBool = true
##### Note:
Line 11th in the Code is the focus. My OCBool type supports the BooleanLiteralConvertible protocol. What does this association do? In Xcode code editor, I hold down the Command key, then, click the BooleanLiteralConvertible protocol name in row 11th to enter its definition,
##### Its definition is as follows:
The Code is as follows:
Protocol BooleanLiteralConvertible {
Typealias BooleanLiteralType
Class func convertFromBooleanLiteral (value: BooleanLiteralType)-> Self
}
There is a class method convertFromBooleanLiteral In this definition. Its parameter is of the BooleanLiteralType, that is, the Bool type that I passed in, and the return value is the type that implements this Protocol. In our OCBool type, the return value is OCBool itself. With this definition, we can initialize the Boolean literal directly for the OCBool type.
#### Supported Bool type determination
My friends are worried about how I use it to implement logical judgment. So if you write this,
##### Sample code:
The Code is as follows:
Var isSuccess: OCBool = true
If isSuccess {
Println ("old code, please eat hot pot! ")
}
You will never eat the hot pot of the old Code, because the compiler will roar here:
The Code is as follows:
/Users/tyrion-OldCoder/Documents/Learning/BoolType/main. swift: 27: 4: Type 'ocbool 'does not conform to protocol 'logicvalue'
Currently, OCBool can only be initialized with the bool type, rather than directly returning the bool type. I still remember that in the old Code-speaking programming vernacular Swift, I mentioned it multiple times, mom no longer worries about the way if a = 1 {} is written. Because the equal sign does not support the return value, the condition after the if judgment must have a return value, but OCBool does not, so the compiler cried. We solve this problem.
##### Sample code:
The Code is as follows:
Import Foundation
Println ("Hello, World! ")
Enum OCBool {
Case ocTrue
Case ocFalse
}
Extension OCBool: BooleanLiteralConvertible {
Static func convertFromBooleanLiteral (value: Bool)-> OCBool {
Return value? OcTrue: ocFalse
}
}
Extension OCBool: LogicValue {
Func getLogicValue ()-> Bool {
Var boolValue: Bool {
Switch self {
Case. ocTrue:
Return true
Case. ocFalse:
Return false
}
}
Return boolValue
}
}
Var isSuccess: OCBool = true
If isSuccess {
Println ("old code, please eat hot pot! ")
}
#### The running result is as follows:
The Code is as follows:
Hello, World!
Please eat hot pot with old code!
Program ended with exit code: 0
##### Note:
● If your friends are using Xcode of Beta version, note that in the official Blog of apple, if row 17th of the Code is incorrect under Xcode Beta4, the protocol here is, logicValue is not BooleanVue, so it is a good habit to remember to check the error message.
● Pay attention to line 1 of the Code, which perfectly supports if judgment, and the output result is "old code, please eat hot pot". The old code is just a saying, so do not take it seriously.
#### Support for various types
Friends, Jianghu risks, a large number of sects, old code has its own OCBool type, maybe Songshan Shaolin has its own SSBool type, and even Guo Meimei may have its own MMBool type, therefore, OCBool must be able to identify these types. All these types can be identified as long as the LogicValue protocol is supported,
##### Sample code:
The Code is as follows:
Extension OCBool {
Init (_ v: LogicValue)
{
If v. getLogicValue (){
Self =. ocTrue
}
Else {
Self =. ocFalse
}
}
}
Var mmResult: Bool = true
Var ocResult: OCBool = OCBool (mmResult)
If ocResult {
Println ("Old Code has no money. Guo Meimei invites you to eat hot pot! ")
}
##### The code running result is as follows:
The Code is as follows:
Hello, World!
Old Code has no money. Guo Meimei invites you to eat hot pot!
Program ended with exit code: 0
Pretty! Our OCBool type now supports initialization of all logical variables.
##### Note:
● The usage of the lower horizontal bar of line 2nd in the Code: "_" is a powerful little strong. The purpose of this function is to shield external parameter names, so friends can directly: var ocResult: OCBool = OCBool (mmResult) instead of: var ocResult: OCBool = OCBool (v: mmResult), friends are stunned! There is no external parameter name in this init function. I still remember the old code in the book. The Swift initialization function uses the internal parameter name by default as the External Parameter Name.
#### Improve the OCBool Boolean gene system:
Friends, the value of the bool type lies in various judgments, such as = ,! =, &, |, ^ ,!, We also need OCBool to provide these functions for a variety of combined logical operations. Otherwise, we will have genetic defects and see how the old code is implemented:
The Code is as follows:
Extension OCBool: Equatable {
}
// Supports equivalent judgment Operators
Func = (left: OCBool, right: OCBool)-> Bool {
Switch (left, right ){
Case (. ocTrue,. ocTrue ):
Return true
Default:
Return false
}
}
// Supports bitwise AND Operators
Func & (left: OCBool, right: OCBool)-> OCBool {
If left {
Return right
}
Else {
Return false
}
}
// Supports bitwise OR Operators
Func | (left: OCBool, right: OCBool)-> OCBool {
If left {
Return true
}
Else {
Return right
}
}
// Supports bitwise OR Operators
Func ^ (left: OCBool, right: OCBool)-> OCBool {
Return OCBool (left! = Right)
}
// Supports reverse Operators
@ Prefix func! (A: OCBool)-> OCBool {
Return a ^ true
}
// Supports combination and operator
Func & = (inout left: OCBool, right: OCBool ){
Left = left & right
}
Var isHasMoney: OCBool = true
Var isHasWife: OCBool = true
Var isHasHealty: OCBool = true
Var isHasLover: OCBool = true
IsHasMoney! = IsHasHealty
IsHasHealty = isHasMoney
IsHasWife ^ isHasLover
IsHasWife =! IsHasLover
If (isHasMoney | isHasHealty) & isHasHealty {
Println ("the winner of life, just like the old code! ")
} Else
{
Println! ")
}
Now, the thunder outside the window woke up the old code, and now we should eat it. The old code above shows you how to create your own type, I remember that the example of the old Code was tested in Xcode6 Beta4. As for the change of Beta5, we have not yet discussed it. We hope that you can practice it well. In the future, all kinds of custom types will be based on this idea. This chapter is not original. I carefully read Apple's official blog and summarized my exercises, please contact du Niang Google♂♂♂