@@ -21,19 +21,28 @@ Swift introduces advanced types such as tuples.
2121Tuples enable you to create and pass around groupings of values.
2222You can use a tuple to return multiple values from a function as a single compound value.
2323
24- Swift also introduces optional types,
25- which handle the absence of a value .
26- Optionals say either “there * is* a value, and it equals * x* ”
24+ Swift handles the absence of a value
25+ using optional types .
26+ Optionals say either “there * is* a value, which is * x* ”
2727or “there * isn't* a value at all”.
28-
29- Swift is a * type-safe* language,
30- which means the language helps you to be clear about the types of values your code can work with.
28+ Optionals ensure that code always
29+ checks whether a value is missing before using the value,
30+ and non-optional values are guaranteed to never be missing.
31+
32+ Swift is a safe language,
33+ which means it makes it easier for you find and fix several categories of bugs
34+ as early as possible during the development process,
35+ and lets you guarantee that certain kinds of bugs can't happen.
36+ Type safety enables you to be clear about
37+ the types of values your code works with.
3138If part of your code requires a ` String ` ,
3239type safety prevents you from passing it an ` Int ` by mistake.
33- Likewise, type safety prevents you from
34- accidentally passing an optional ` String `
35- to a piece of code that requires a non-optional ` String ` .
36- Type safety helps you catch and fix errors as early as possible in the development process.
40+ Memory safety ensures that you work with valid data only,
41+ not uninitialized memory or deinitialized objects,
42+ and ensures that you work with that data in safe ways ---
43+ even in programs that run multiple pieces of code at the same time.
44+ Swift performs most of its safety checks while building your code,
45+ and in some cases performs additional checks while your code is running.
3746
3847## Constants and Variables
3948
@@ -561,17 +570,28 @@ Swift provides two signed floating-point number types:
561570
562571## Type Safety and Type Inference
563572
564- Swift is a * type-safe* language.
565- A type safe language encourages you to be clear about
566- the types of values your code can work with.
567- If part of your code requires a ` String ` , you can't pass it an ` Int ` by mistake.
573+ Every value in a Swift program has a type.
574+ Every place you store a value ---
575+ including constants, variables, and properties ---
576+ also has a type.
577+ You might write the type explicitly using a type annotation,
578+ or Swift might infer the type from an initial value.
579+ Every place in your code where you provide a value,
580+ that value's type must match the place you use it.
581+ For example,
582+ if part of your code requires a ` String ` ,
583+ you can't pass it an ` Int ` by mistake.
584+ This kind of checking makes Swift a * type-safe* language.
568585
569- Because Swift is type safe,
570- it performs * type checks* when compiling your code
586+ A type safe language encourages you to be clear about
587+ the types of values your code works with.
588+ Values of one type are never implicitly converted to another type.
589+ However, some types can be explicitly converted.
590+ When building code,
591+ Swift checks the code for type safety
571592and flags any mismatched types as errors.
572- This enables you to catch and fix errors as early as possible in the development process.
573593
574- Type- checking helps you avoid errors when you're working with different types of values.
594+ Type checking helps you avoid errors when you're working with different types of values.
575595However, this doesn't mean that you have to specify the type of
576596every constant and variable that you declare.
577597If you don't specify the type of value you need,
@@ -1000,8 +1020,6 @@ they were initialized with Boolean literal values.
10001020As with ` Int ` and ` Double ` above,
10011021you don't need to declare constants or variables as ` Bool `
10021022if you set them to ` true ` or ` false ` as soon as you create them.
1003- Type inference helps make Swift code more concise and readable
1004- when it initializes constants or variables with other values whose type is already known.
10051023
10061024Boolean values are particularly useful when you work with conditional statements
10071025such as the ` if ` statement:
@@ -1076,7 +1094,7 @@ if i == 1 {
10761094-->
10771095
10781096The result of the ` i == 1 ` comparison is of type ` Bool ` ,
1079- and so this second example passes the type-check .
1097+ and so this second example passes the type checking .
10801098Comparisons like ` i == 1 ` are discussed in < doc:BasicOperators > .
10811099
10821100As with other examples of type safety in Swift,
@@ -1778,6 +1796,58 @@ if let definiteString = assumedString {
17781796 ```
17791797-->
17801798
1799+ ## Memory Safety
1800+
1801+ In addition to the checks that prevent type mismatches,
1802+ described above in < doc:TheBasics#Type-Safety-and-Type-Inference > ,
1803+ Swift also protects code against working with invalid memory.
1804+ This protection is known as * memory safety*
1805+ and includes the following requirements:
1806+
1807+ - Values are set before being read.
1808+ The protection against interacting with uninitialized regions of memory
1809+ is also known as * definite initialization* .
1810+ - Arrays and buffers are accessed only at valid indexes.
1811+ The protection against out-of-bounds access
1812+ is also known as * bounds safety* .
1813+ - Memory is accessed only during the value’s lifetime.
1814+ The protection against use-after-free errors
1815+ is also known as * lifetime safety* .
1816+ - Access to memory overlaps only in provably safe ways.
1817+ The protection against possible data races in concurrent code
1818+ is also known as * thread safety* .
1819+
1820+ If you've worked in languages that don't provide these guarantees,
1821+ you may be familiar with some of the errors and bugs
1822+ named in the list above.
1823+ If you haven't encountered these issues, that's ok;
1824+ safe code in Swift avoids these problems.
1825+ For information about how Swift ensures you set initial values,
1826+ see < doc:Initialization > ,
1827+ for information about how Swift checks memory safety in concurrent code,
1828+ see < doc:Concurrency > ,
1829+ and for information about how Swift checks overlapping accesses to memory,
1830+ see < doc:MemorySafety > .
1831+
1832+ Sometimes you need to work outside of the bounds of safety ---
1833+ for example, because of limitations of the language or standard library ---
1834+ so Swift also provides unsafe versions of some APIs.
1835+ When you use types or methods whose name includes words such as
1836+ "unsafe", "unchecked", or "unmanaged",
1837+ you take on the responsibility for safety.
1838+
1839+ Safe code in Swift can still encounter errors and unexpected failures,
1840+ which might stop the program's execution.
1841+ Safety doesn't ensure that your code runs to completion.
1842+ Swift provides several ways to indicate and recover from errors,
1843+ discussed in < doc:TheBasics#Error-Handling >
1844+ and < doc:TheBasics#Assertions-and-Preconditions > below.
1845+ However, in some cases,
1846+ the * only* safe way to handle an error is to stop execution.
1847+ If you need to guarantee that a service never unexpected stops,
1848+ incorporate fault tolerance into its overall architecture,
1849+ so it can recover from any of its components stopping unexpectedly.
1850+
17811851## Error Handling
17821852
17831853You use * error handling* to respond to error conditions
0 commit comments