[Swift] learning notes (1) -- getting started with basic data types, code styles, tuples, assertions, and swift learning notes
Since Apple released swift, I have been trying to learn about it and have never been fully involved in it. From today on, I will use blogs to drive my swift learning, it is also faster for everyone to understand it.
1. Define variables and constants
Var defines variables, and let defines constants.
For example:
Var test = 1 test = 2 // variable can change the value of let test = 1 test = 2 // constant cannot change the value. The Compiler reports the error var test1 = 1, test2 = 2, test3 = 3 // separate multiple variables with commas
2. Add a type Annotation
In the var test = 1 example, test is inferred to be of the int type by swift. Swift is type-safe.
The var test: Int = 1 definition is the same as above, but a type annotation is added to the test variable, telling swfit not to deduce it.
3. Basic Data Types
Int indicates the integer value, Double and Float indicates the floating point value, Bool indicates the Boolean value, String indicates the text data, and Character indicates the Character type. Swift also provides two basic collection types: Array and Dictionary.
4. Global output functions println and print
As we all know, the difference between line feed and non-line feed is output to the console.
Example: println ("this is my first swift test ")
What should I do if I want to output the test defined above?
Println ("test value = \ (test )")
Swift uses string interpolation to add a constant or variable name to a long string as a placeholder. Swift replaces these placeholders with the values of the current constant or variable, is \()
5. Comments // when the line comments/**/fragment comments
6. Do not use semicolons?
Example: let test = "test"; println (test)
7. Integer integers are divided into signed (positive, negative, 0) and unsigned (positive, 0)
Swift provides 8, 16, 32, and 64-Bit Signed and unsigned integer types. These integer types are similar to those in the C language. For example, the 8-bit unsigned integer type is UInt8, and the 32-bit signed integer type is Int32. Like other types of Swift, the integer type adopts the upper-case naming method.
Let minValue = UInt8.min // The value of minValue is 0, which is the minimum value of UInt8 type. let maxValue = UInt8.max // The value of maxValue is 255, which is the maximum value of UInt8 type.
Int and UInt
Generally, you do not need to specify the length of an integer. Swift provides a special integer Int with the same length as the native character of the current platform:
On a 32-bit platform, Int and Int32 are of the same length.
On a 64-bit platform, Int and Int64 are of the same length.
Swift also provides a special unsigned UInt with the same length as the native character of the current platform:
On a 32-bit platform, UInt and UInt32 are of the same length.
On a 64-bit platform, UInt and UInt64 are of the same length.
8. Floating Point Number
Double and Float
A floating point number is a number with a decimal part.
The floating point type has a larger range than the integer type. It can store numbers that are larger or smaller than the Int type. Swift provides two types of signed floating point numbers:
Double indicates a 64-bit floating point number. Use this type when you need to store large or high-precision floating point numbers.
Float indicates a 32-bit floating point number. This type can be used if the precision requirement is not high.
Double has a high precision, with at least 15 digits while Float has at least 6 digits. The type you select depends on the range of values to be processed by your code.
9. Numeric literal
An integer literal can be written as follows:
One decimal number with no prefix
A binary number with a prefix of 0 B
An octal number with a prefix of 0 o
A hexadecimal number with a prefix of 0x
The floating point literal can be in decimal format (without a prefix) or hexadecimal format (with a prefix of 0x ). There must be at least one decimal number (or a hexadecimal number) on both sides of the decimal point ). The floating point literal also has an optional index (exponent), which is specified in the decimal floating point number through the upper or lower case e. It is specified in the hexadecimal floating point number through the upper or lower case p.
If the exponent of a decimal number is exp, the number is equivalent to the product of base number and 10 ^ exp:
1.25e2 indicates 1.25x10 ^ 2, or 125.0.
1.25e-2 indicates 1.25x10 ^-2, which is equal to 0.0125.
If the exponent of a hexadecimal number is exp, the number is equivalent to the product of the base number and 2 ^ exp:
0xFp2 indicates 15 × 2 ^ 2, equal to 60.0.
0xFp-2 indicates 15 × 2 ^-2, equal to 3.75.
The following floating point literal is equal to 12.1875 in decimal format:
Let decimalDouble = 12.1875
Let exponentDouble = 1.21875e1
Let hexadecimalDouble = 0xC. 3p0
The numeric literal can include additional formats to enhance readability. Both integers and floating-point numbers can be added with extra zeros and underscores, without affecting the literal volume:
Let paddedDouble = 000123.456
Let oneMillion = 0000000_000
Let justOverOneMillion = 0000000_000.000_000_1
It seems to be a headache. It would be nice to use Decimal for any trouble... It's still a bit useful...
10. type conversion of Character Types
Var one: Int = 10var two = 3.11var three = Double (one) + two // Double has an integer constructor. Other integer types are the same. Here is an example.
11. boolean value true false
12. Type alias typealias
It doesn't make much sense to change the name of an integer. Poor readability .. For example
typealias MyInt = Intvar test: MyInt = 123
13. tuples are very important.
Tuples combines multiple values into a compound value. The value in the meta-group can be of any type, but it is not required to be of the same type.
For example, create a (Int, String) type tuples: (500, "server error ")
Let Mytuples = (500, "server error") // define a tuples let (code, message) = Mytuples // break the tuples into output let (code ,_) = Mytuples // only the first value is required. Do not use _ to represent println ("code is \ (code )") // output 500 println ("code is \ (Mytuples.0)") // access and output 500let Mytuples2 through subscript = (code: 500, message: "server error ") // define a parameter-name tuples println ("code is \ (Mytuples2.code)"); // access the output 500 using the element name of the tuples
14. Optional
Optional type (optionals) is used to handle possible missing values. Optional type:
Value, equal to x
Or
No value nil
Let possibleNumber = "123"
Let convertedNumber = possibleNumber. toInt ()
// ConvertedNumber is assumed to be of the type "Int? ", Or type" optional Int"
Because the toInt method may fail, it returns an optional type (optional) Int instead of an Int. An optional Int is written as an Int? Instead of Int. The question mark implies that the included values are optional, that is, they may contain Int values or not. (It cannot contain any other value, such as a Bool value or a String value. It can only be Int or nothing .)
If convertedNumber {println ("\ (possibleNumber) has an integer value of \ (convertedNumber !) ")} Else {println (" \ (possibleNumber) cocould not be converted to an integer ")} // output" 123 has an integer value of 123"
You can add an exclamation point (
!
) To obtain the value.
The above is a lot of trouble. You have to judge whether the variable has a value: use if to judge and use it again! Force resolution.
You can use optional binding to simplify the Code:
If let actualNumber = possibleNumber. toInt () {println ("\ (possibleNumber) has an integer value of \ (actualNumber)")} else {println ("\ (possibleNumber) cocould not be converted to an integer ")} // output" 123 has an integer value of 123"
You can also use implicit parsing of optional types for processing (to ensure that there is always a value for the variable, otherwise there will be an error)
Let assumedString: String! = "An implicitly unwrapped optional string." println (assumedString) // No exclamation point is required // output "An implicitly unwrapped optional string ."
15. assertion judgment logic true continue execution false end application
For example, let a = 1; assert (a> = 0) triggers
Convert the following code snippet to the intermediate code of the Four-tuple expression.
The rapid development has made them an important part of modern information technology. It is the foundation and core of computer information systems and computer application systems. For any enterprise, data is an important asset of the enterprise. How to use the data effectively plays an extremely important role in the development of the enterprise. With the rapid development of China's market economy and the continuous improvement of people's living standards, the number of trees in the library has gradually increased, which also challenges the technology of library management, the previous manual management methods no longer adapt to the current environment. Instead, they are replaced by an advanced library management system, the library management system created using PowerBuilder allows managers to conveniently and quickly manage, query, borrow, and input books.
2. Demand Analysis
2.1 system objectives
The library management information system is a typical information management system (MIS). Its development mainly includes the establishment and maintenance of background databases and the development of front-end applications. The former requires the establishment of a database with strong data consistency and integrity and good data security. However, the latter requires the application to be fully functional and easy to use.
The overall task of system development is to systematize, standardize, and automate various types of information.
2.2 requirement definition
Library management system development. The overall design goal of system development is to systematize, standardize, and automate book management and achieve centralized and unified management of books and materials.
This system mainly manages library information. Its main functions are to manage information about readers, books, borrowing, querying, deleting, and administrators. The system structure is divided into reader category management, reader archive management, library type management, library archive management, and book process management. reader management can browse reader information and maintain reader information. Book Management allows you to browse and maintain book information. The lending management can display the borrowing status of books in the current database and maintain the borrowing information. The system mainly solves the problem of using keywords to query databases.
The functional modules of the system are shown as follows:
Figure 2-1 system function module diagram
To meet the requirements of the general library management information system, analyze the content and data flow of the book management process and design the data items as shown below:
Reader information
Attribute: reader student ID, reader name, reader gender, contact number, department, effective date, expiration date, Violation Status, accumulative borrowing
Primary Key: reader student ID
Books
Attributes: ISBN, title, author, publisher, publication date, Introduction
Primary Key: ISBN
Administrator Information
Attribute: work ID, name, gender, phone number, home address
Primary Key: Work number
2.3 Data Process
2.3.1 readers
As a student, the requirements for the library management system are as follows:
1. The library's collection information can be queried in various ways (such as the title, number, and author.
2. Books can be easily borrowed, renewed, and returned.
3. Ability to query basic information and borrow books.
4. Be familiar with the use of the library management system.
The reader's workflow for entering the system is as follows:
2-1 flowchart for entering the system
2.3.2 Librarians
As librarians, they have the following requirements for the library management system:
1. Easy entry and registration of books, and cancellation of old books.
2. It is convenient to register new students or cancel graduation student information (basic information and borrowing information ).
3. Information such as the expiration time of books borrowed by students of various colleges, library information in the library, and borrowing information can be posted at any time, so that the colleges can learn some borrowing information of the students of this school at any time.
The workflow of the librarians is as follows:
2-2 workflow of the librarians
3. Function Description
System function analysis is based on the overall task of system development. The main functions required by the system are as follows:
(1) Basic operations such as entry, modification, and deletion of books.
1. Develop the standards for book categories and enter category information, including Category numbers, category names, keywords, and remarks.
2. query and modify the book category information, including Category numbers, category names, keywords, and remarks.
3. Enter the book information, including the book number, book name, book category, author name, publisher name,... the remaining full text>