SWIFTDOCS-01-the-basics

所属分类:MacOS编程
开发工具:Swift
文件大小:0KB
下载次数:0
上传日期:2022-10-22 13:47:17
上 传 者sh-1993
说明:  SWIFT文件→ Swift是一种用于iOS、macOS、watchOS和tvOS应用程序开发的新编程语言。尽管如此,Swift的许多部分...,
(SWIFT DOCUMENTATION → Swift is a new programming language for iOS, macOS, watchOS, and tvOS app development. Nonetheless, many parts of Swift will be familiar from your experience of developing in C and Objective-C.)

文件列表:
.DS_Store (8196, 2023-10-20)
The Basics.playground/ (0, 2023-10-20)
The Basics.playground/Contents.swift (48280, 2023-10-20)
The Basics.playground/Resources/ (0, 2023-10-20)
The Basics.playground/Resources/swift-logo.png (15225, 2023-10-20)
The Basics.playground/contents.xcplayground (236, 2023-10-20)

![Swift](https://github.com/MatthewpHarding/SWIFTDOCS-01-the-basics/blob/master/readme-images/swift-logo.png) ![Xcode Playground](https://github.com/MatthewpHarding/SWIFTDOCS-01-the-basics/blob/master/readme-images/xcode-icon.png) # Page 1 [The Basics](https://github.com/MatthewpHarding/SWIFTDOCS-01-the-basics/blob/master/https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics) Swift v5.7 | [Swift.org](https://github.com/MatthewpHarding/SWIFTDOCS-01-the-basics/blob/master/https://docs.swift.org) | [The official Swift Language Guide](https://github.com/MatthewpHarding/SWIFTDOCS-01-the-basics/blob/master/https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics) converted to Swift playgrounds. The code attached is page 1 of [The official Swift Language Guide](https://github.com/MatthewpHarding/SWIFTDOCS-01-the-basics/blob/master/https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics) converted to a Swift playground making every code example editable and executable. Try it by cloning or downloading the repo and opening the `.playground` file in Xcode. ---------------------- Swift is a new programming language for iOS, macOS, watchOS, and tvOS app development. Nonetheless, many parts of Swift will be familiar from your experience of developing in C and Objective-C. Swift provides its own versions of all fundamental C and Objective-C types, including `Int` for integers, `Double` and `Float` for floating-point values, `Bool` for Boolean values, and `String` for textual data. Swift also provides powerful versions of the three primary collection types, `Array`, `Set`, and `Dictionary`, as described in Collection Types. Like C, Swift uses variables to store and refer to values by an identifying name. Swift also makes extensive use of variables whose values can’t be changed. These are known as constants, and are much more powerful than constants in C. Constants are used throughout Swift to make code safer and clearer in intent when you work with values that don’t need to change. In addition to familiar types, Swift introduces advanced types not found in Objective-C, such as tuples. Tuples enable you to create and pass around groupings of values. You can use a tuple to return multiple values from a function as a single compound value. Swift also introduces optional types, which handle the absence of a value. Optionals say either “there is a value, and it equals x” or “there isn’t a value at all”. Using optionals is similar to using nil with pointers in Objective-C, but they work for any type, not just classes. Not only are optionals safer and more expressive than nil pointers in Objective-C, they’re at the heart of many of Swift’s most powerful features. Swift is a type-safe language, which means the language helps you to be clear about the types of values your code can work with. If part of your code requires a `String`, type safety prevents you from passing it an `Int` by mistake. Likewise, type safety prevents you from accidentally passing an optional `String` to a piece of code that requires a non-optional `String`. Type safety helps you catch and fix errors as early as possible in the development process. ## Constants and Variables Constants and variables associate a name (such as `maximumNumberOfLoginAttempts` or `welcomeMessage`) with a value of a particular type (such as the number 10 or the `String` "Hello"). The value of a constant can’t be changed once it’s set, whereas a variable can be set to a different value in the future. ### Declaring Constants and Variables Constants and variables must be declared before they’re used. You declare constants with the `let` keyword and variables with the `var` keyword. Here’s an example of how constants and variables can be used to track the number of login attempts a user has made: ```Swift let maximumNumberOfLoginAttempts = 10 var currentLoginAttempt = 0 ``` This code can be read as: “Declare a new constant called `maximumNumberOfLoginAttempts`, and give it a value of 10. Then, declare a new variable called `currentLoginAttempt`, and give it an initial value of 0.” In this example, the maximum number of allowed login attempts is declared as a constant, because the maximum value never changes. The current login attempt counter is declared as a variable, because this value must be incremented after each failed login attempt. You can declare multiple constants or multiple variables on a single line, separated by commas: ```Swift var x = 0.0, y = 0.0, z = 0.0 ``` >Note > >→ If a stored value in your code won’t change, always declare it as a constant with the let keyword. Use variables only for storing values that need to be able to change. ### Type Annotations You can provide a type annotation when you declare a constant or variable, to be clear about the kind of values the constant or variable can store. Write a type annotation by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use. This example provides a type annotation for a variable called `welcomeMessage`, to indicate that the variable can store String values: ```Swift var welcomeMessage: String ``` The colon in the declaration means “…of type…,” so the code above can be read as: “Declare a variable called welcomeMessage that’s of type String.” The phrase “of type String” means “can store any String value.” Think of it as meaning “the type of thing” (or “the kind of thing”) that can be stored. The `welcomeMessage` variable can now be set to any string value without error: ```Swift welcomeMessage = "Hello" ``` You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name: ```Swift var red, green, blue: Double ``` > Note > >→ It’s rare that you need to write type annotations in practice. If you provide an initial value for a constant or variable at the point that it’s defined, Swift can almost always infer the type to be used for that constant or variable, as described in Type Safety and Type Inference. In the `welcomeMessage` example above, no initial value is provided, and so the type of the `welcomeMessage` variable is specified with a type annotation rather than being inferred from an initial value. ### Naming Constants and Variables Constant and variable names can contain almost any character, including Unicode characters: ```Swift let π = 3.14159 let 你好 = "你好世界" let = "dogcow" ``` Constant and variable names can’t contain whitespace characters, mathematical symbols, arrows, private-use Unicode scalar values, or line- and box-drawing characters. Nor can they begin with a number, although numbers may be included elsewhere within the name. Once you’ve declared a constant or variable of a certain type, you can’t declare it again with the same name, or change it to store values of a different type. Nor can you change a constant into a variable or a variable into a constant. > Note > >→ If you need to give a constant or variable the same name as a reserved Swift keyword, surround the keyword with backticks (`) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice. You can change the value of an existing variable to another value of a compatible type. In this example, the value of `friendlyWelcome` is changed from "Hello!" to "Bonjour!": ```Swift var friendlyWelcome = "Hello!" friendlyWelcome = "Bonjour!" // friendlyWelcome is now "Bonjour!" ``` Unlike a variable, the value of a constant can’t be changed after it’s set. Attempting to do so is reported as an error when your code is compiled: ```Swift let languageName = "Swift" languageName = "Swift++" // This is a compile-time error: languageName cannot be changed. ``` ### Printing Constants and Variables You can print the current value of a constant or variable with the `print(_:separator:terminator:)` function: ```Swift print(friendlyWelcome) // Prints "Bonjour!" ``` The `print(_:separator:terminator:)` function is a global function that prints one or more values to an appropriate output. In Xcode, for example, the `print(_:separator:terminator:)` function prints its output in Xcode’s “console” pane. The separator and terminator parameter have default values, so you can omit them when you call this function. By default, the function terminates the line it prints by adding a line break. To print a value without a line break after it, pass an empty string as the terminator—for example, `print(someValue, terminator: "")`. For information about parameters with default values, see Default Parameter Values. Swift uses string interpolation to include the name of a constant or variable as a placeholder in a longer string, and to prompt Swift to replace it with the current value of that constant or variable. Wrap the name in parentheses and escape it with a backslash before the opening parenthesis: ```Swift print("The current value of friendlyWelcome is \(friendlyWelcome)") // Prints "The current value of friendlyWelcome is Bonjour!" ``` > Note > >→ All options you can use with string interpolation are described in String Interpolation. ## Comments Use comments to include nonexecutable text in your code, as a note or reminder to yourself. Comments are ignored by the Swift compiler when your code is compiled. Comments in Swift are very similar to comments in C. Single-line comments begin with two forward-slashes (//): ```Swift // This is a comment. //: Multiline comments start with a forward-slash followed by an asterisk (/*) and end with an asterisk followed by a forward-slash (*/): /* This is also a comment but is written over multiple lines. */ ``` Unlike multiline comments in C, multiline comments in Swift can be nested inside other multiline comments. You write nested comments by starting a multiline comment block and then starting a second multiline comment within the first block. The second block is then closed, followed by the first block: ```Swift /* This is the start of the first multiline comment. /* This is the second, nested multiline comment. */ This is the end of the first multiline comment. */ ``` Nested multiline comments enable you to comment out large blocks of code quickly and easily, even if the code already contains multiline comments. ## Semicolons Unlike many other languages, Swift doesn’t require you to write a semicolon (;) after each statement in your code, although you can do so if you wish. However, semicolons are required if you want to write multiple separate statements on a single line: ```Swift let cat = ""; print(cat) // Prints "" ``` ## Integers Integers are whole numbers with no fractional component, such as 42 and -23. Integers are either signed (positive, zero, or negative) or unsigned (positive or zero). Swift provides signed and unsigned integers in 8, 16, 32, and 64 bit forms. These integers follow a naming convention similar to C, in that an 8-bit unsigned integer is of type UInt8, and a 32-bit signed integer is of type Int32. Like all types in Swift, these integer types have capitalized names. ### Integer Bounds You can access the minimum and maximum values of each integer type with its `min` and `max` properties: ```Swift let minValue = UInt8.min // minValue is equal to 0, and is of type UInt8 let maxValue = UInt8.max // maxValue is equal to 255, and is of type UInt8 ``` The values of these properties are of the appropriate-sized number type (such as UInt8 in the example above) and can therefore be used in expressions alongside other values of the same type. ### Int In most cases, you don’t need to pick a specific size of integer to use in your code. Swift provides an additional integer type, Int, which has the same size as the current platform’s native word size: * On a 32-bit platform, Int is the same size as Int32. * On a 64-bit platform, Int is the same size as Int64. Unless you need to work with a specific size of integer, always use `Int` for integer values in your code. This aids code consistency and interoperability. Even on 32-bit platforms, Int can store any value between -2,147,483,648 and 2,147,483,647, and is large enough for many integer ranges. ### UInt Swift also provides an unsigned integer type, `UInt`, which has the same size as the current platform’s native word size: * On a 32-bit platform, `UInt` is the same size as `UInt32`. * On a 64-bit platform, `UInt` is the same size as `UInt64`. > Note > >→ Use `UInt` only when you specifically need an unsigned integer type with the same size as the platform’s native word size. If this isn’t the case, `Int` is preferred, even when the values to be stored are known to be nonnegative. A consistent use of `Int` for integer values aids code interoperability, avoids the need to convert between different number types, and matches integer type inference, as described in Type Safety and Type Inference. ## Floating-Point Numbers Floating-point numbers are numbers with a fractional component, such as 3.14159, 0.1, and -273.15. Floating-point types can represent a much wider range of values than integer types, and can store numbers that are much larger or smaller than can be stored in an `Int`. Swift provides two signed floating-point number types: * `Double` represents a 64-bit floating-point number. * `Float` represents a 32-bit floating-point number. > Note > >→ `Double` has a precision of at least 15 decimal digits, whereas the precision of `Float` can be as little as 6 decimal digits. The appropriate floating-point type to use depends on the nature and range of values you need to work with in your code. In situations where either type would be appropriate, `Double` is preferred. ## Type Safety and Type Inference Swift is a type-safe language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code requires a `String`, you can’t pass it an Int by mistake. Because Swift is type safe, it performs type checks when compiling your code and flags any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process. Type-checking helps you avoid errors when you’re working with different types of values. However, this doesn’t mean that you have to specify the type of every constant and variable that you declare. If you don’t specify the type of value you need, Swift uses type inference to work out the appropriate type. Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide. Because of type inference, Swift requires far fewer type declarations than languages such as C or Objective-C. Constants and variables are still explicitly typed, but much of the work of specifying their type is done for you. Type inference is particularly useful when you declare a constant or variable with an initial value. This is often done by assigning a literal value (or literal) to the constant or variable at the point that you declare it. (A literal value is a value that appears directly in your source code, such as 42 and 3.14159 in the examples below.) For example, if you assign a literal value of 42 to a new constant without saying what type it is, Swift infers that you want the constant to be an `Int`, because you have initialized it with a number that looks like an integer: ```Swift let meaningOfLife = 42 // meaningOfLife is inferred to be of type Int ``` Likewise, if you don’t specify a type for a floating-point literal, Swift infers that you want to create a `Double`: ```Swift let pi = 3.14159 // pi is inferred to be of type Double ``` Swift always chooses `Double` (rather than `Float`) when inferring the type of floating-point numbers. If you combine integer and floating-point literals in an expression, a type of `Double` will be inferred from the context: ```Swift let anotherPi = 3 + 0.14159 // anotherPi is also inferred to be of type Double ``` The literal value of 3 has no explicit type in and of itself, and so an appropriate output type of `Double` is inferred from the presence of a floating-point literal as part of the addition. ## Numeric Literals Integer literals can be written as: * A decimal number, with no prefix * A binary number, with a 0b prefix * An octal number, with a 0o prefix * A hexadecimal number, with a 0x prefix All of these integer literals have a decimal value of 17: ```Swift let decimalInteger = 17 let binaryInteger = 0b10001 // 17 in binary notation let octalInteger = 0o21 // 17 in octal notation let hexadecimalInteger = 0x11 // 17 in hexadecimal notation ``` Floating-point literals can be decimal (with no prefix), or hexadecimal (with a 0x prefix). They must always have a number (or hexadecimal number) on both sides of the decimal point. `Decimal` floats can also have an optional exponent, indicated by an uppercase or lowercase e; hexadecimal floats must have an exponent, indicated by an uppercase or lowercase p. For decimal numbers with an exponent of exp, the base number is multiplied by 10exp: * 1.25e2 means 1.25 x 102, or 125.0. * 1.25e-2 means 1.25 x 10-2, or 0.0125. For hexadecimal numbers with an exponent of exp, the base number is multiplied by 2exp: * 0xFp2 means 15 x 22, or 60.0. * 0xFp-2 means 15 x 2-2, or 3.75. All of these floating-point literals have a decimal value of 12.1875: ```Swift let decimalDouble = 12.1875 let exponentDouble = 1.21875e1 let hexadecimalDouble = 0xC.3p0 ``` Numeric literals can contain extra formatting to make them easier to read. Both integers and floats can be padded with extra zeros and can contain underscores to help with readability. Neither type of formatting affects the underlying value of the literal: ```Swift let paddedDouble = 000123.456 let oneMillion = 1_000_000 let justOverOneMillion = 1_000_000.000_000_1 ``` ## Numeric Type Conversion Use the `Int` type for all general-purpose integer constants and variables in your code, even if they’re known to be nonnegative. Using the default integer type in everyday situations means that integer constants and variables are immediately interoperable in your code and will match the inferred type for integer literal values. Use other integer types only when they’re specifically needed for the task at hand, because of explicitly sized data from an external source, or for performance, memory usage, or other necessary optimization. Using explicitly sized types in these situations helps to catch any accidental value overflows and implicitly documents the nature of the data being used. ### Integer Conversion The range of numbers that can be stored in an integer constant or variable is different for each numeric type. An `Int8` constant or variable can store numbers between -128 and 127, whereas a `UInt8` constant or variable can store numbers between 0 and 255. A number that won’t fit into a constant or variable of a sized integer type is reported as an error when your code is compiled: ```Swift let cannotBeNegative: UInt8 = -1 // UInt8 can't store negative numbers, and so this will report an error let tooBig: Int8 = Int8.max + 1 // Int8 can't store a number larger than its maximum value, // and so this will also report an error ``` Because each numeric type can store a different range of values, you must opt in to numeric type conversion on a case-by-case basis. This opt-in approach prevents hidden conversion errors and helps make type conversion intentions explicit in your code. To convert one specific number type to another, you initialize a new number of the desired type with the existing value. In the example below, the constant twoThousand is of type `UInt16`, whereas the constant one is of type `UInt8`. They can’t be added together directly, because they’re not of the same type. ... ...

近期下载者

相关文件


收藏者