Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. En dashes
In this articleUse an en dash:
Use an en dash (–) instead of a hyphen in a compound modifier when:
Don’t use an en dash to indicate an empty cell in a table. Don’t use spaces on either side of an en dash. Exception Surround an en dash with spaces when it's used as a minus sign in an equation or in a time stamp appearing in UI. In text, don’t use an en dash in a range of times. Use to instead: 10:00 AM to 2:00 PM. In a schedule or listing, use an en dash with no spaces around it: 10:00 AM–2:00 PM. Exception In a date range that includes two times and two dates, add spaces around the en dash. See also Date and time term collection FeedbackSubmit and view feedback for Additional resourcesAdditional resourcesIn this articleThe Basics¶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 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 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 Constants and Variables¶Constants and variables associate a name (such as Declaring Constants and Variables¶Constants and variables must be declared before they’re used. You declare constants with the
This code can be read as: “Declare a new constant called 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:
Note If a stored value in your code won’t change, always declare it as a constant with the 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
The colon in the declaration means “…of type…,” so the code above can be read as: “Declare a variable called The phrase “of type The
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:
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 Naming Constants and Variables¶Constant and variable names can contain almost any character, including Unicode characters:
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 ( You can change the value of an existing variable to another value of a compatible type. In this example,
the value of
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:
Printing Constants and Variables¶You can print the current value of a constant or variable with the
The 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:
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 (
Multiline comments start with a forward-slash followed by an asterisk (
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:
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 (
Integers¶Integers are whole numbers with no fractional component, such as 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 Integer Bounds¶You can access the minimum and
maximum values of each integer type with its
The values of these properties are of the appropriate-sized number type (such as 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,
Unless you need to work with a specific size of integer, always use UInt¶Swift also provides an unsigned integer type,
Note Use Floating-Point Numbers¶Floating-point numbers are numbers with a fractional component, such as 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
Note
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 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 For example, if you assign a literal value of
Likewise, if you don’t specify a type for a floating-point literal, Swift infers that you want to create a
Swift always chooses If you combine integer and floating-point literals in an expression, a type of
The literal value of Numeric Literals¶Integer literals can be written as:
All of these integer literals have a decimal value of
Floating-point literals can be decimal (with no prefix), or hexadecimal (with a For decimal numbers with an exponent of
For hexadecimal numbers with an exponent of
All of these floating-point literals have a decimal value of
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:
Numeric Type Conversion¶Use the 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
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
Because both sides of the addition are now of type
Integer and Floating-Point Conversion¶Conversions between integer and floating-point numeric types must be made explicit:
Here, the value of the constant Floating-point to integer conversion must also be made explicit. An integer type can be initialized with a
Floating-point values are always truncated when used to initialize a new integer value in this way. This means that Note The rules for combining numeric constants and variables are different from the rules for numeric literals. The literal
value Type Aliases¶Type aliases define an alternative name for an existing type. You define
type aliases with the Type aliases are useful when you want to refer to an existing type by a name that’s contextually more appropriate, such as when working with data of a specific size from an external source:
Once you define a type alias, you can use the alias anywhere you might use the original name:
Here, Booleans¶Swift has a basic
Boolean type, called
The types of Boolean values are particularly useful when you work with conditional statements such as the
Conditional statements such as the Swift’s type safety prevents non-Boolean values from being substituted for
However, the alternative example below is valid:
The result of the As with other examples of type safety in Swift, this approach avoids accidental errors and ensures that the intention of a particular section of code is always clear. Tuples¶Tuples group multiple values into a single compound value. The values within a tuple can be of any type and don’t have to be of the same type as each other. In this example,
The You can create tuples from any permutation of types, and they can contain as many different types as you like. There’s nothing stopping you from having a tuple of type You can decompose a tuple’s contents into separate constants or variables, which you then access as usual:
If you only need some of the tuple’s values, ignore parts of the tuple with an underscore (
Alternatively, access the individual element values in a tuple using index numbers starting at zero:
You can name the individual elements in a tuple when the tuple is defined:
If you name the elements in a tuple, you can use the element names to access the values of those elements:
Tuples are particularly useful as the return values of functions. A function that tries to retrieve a web
page might return the Note Tuples are useful for simple groups of related values. They’re not suited to the creation of complex data structures. If your data structure is likely to be more complex, model it as a class or structure, rather than as a tuple. For more information, see Structures and Classes. Optionals¶You use optionals in situations where a value may be absent. An optional represents two possibilities: Either there is a value, and you can unwrap the optional to access that value, or there isn’t a value at all. Note The concept of optionals doesn’t exist in C or Objective-C. The nearest thing in Objective-C is
the ability to return Here’s an example of how optionals can be used to cope with the absence of a value. Swift’s The example below uses the initializer to try to convert a
Because the initializer might fail, it returns an optional
nil¶You set an optional variable to a valueless state by assigning it the special value
Note You can’t use If you define an optional variable without providing a default value, the variable is automatically set to
Note Swift’s If Statements and Forced Unwrapping¶You can use an If an optional has a value, it’s considered to be “not equal to”
Once you’re sure that the optional does contain a value, you can access its underlying value by adding an exclamation point (
For more about the Note Trying to use Optional Binding¶You use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable. Optional binding can be used with Write an optional binding for an
You can rewrite the
This code can be read as: “If the optional If the conversion is successful, the If you don’t need to refer to the original, optional constant or variable after accessing the value it contains, you can use the same name for the new constant or variable:
This code starts by checking whether Because this kind of code is so common, you can use a shorter spelling to unwrap an optional value: write just the name of the constant or variable that you’re unwrapping. The new, unwrapped constant or variable implicitly uses the same name as the optional value.
You can use both constants and variables with optional binding. If you wanted to manipulate the value of You can include as many optional bindings and Boolean conditions in a single
Note Constants and variables created with optional binding in an Implicitly Unwrapped Optionals¶As described above, optionals indicate that a constant or
variable is allowed to have “no value”. Optionals can be checked with an Sometimes it’s clear from a program’s structure that an optional will always have a value, after that value is first set. In these cases, it’s useful to remove the need to check and unwrap the optional’s value every time it’s accessed, because it can be safely assumed to have a value all of the time. These kinds of optionals are defined as implicitly unwrapped optionals. You write an implicitly unwrapped optional by placing an exclamation point ( Implicitly unwrapped optionals are useful when an optional’s value is confirmed to exist immediately after the optional is first defined and can definitely be assumed to exist at every point thereafter. The primary use of implicitly unwrapped optionals in Swift is during class initialization, as described in Unowned References and Implicitly Unwrapped Optional Properties. An implicitly
unwrapped optional is a normal optional behind the scenes, but can also be used like a non-optional value, without the need to unwrap the optional value each time it’s accessed. The following example shows the difference in behavior between an optional string and an implicitly unwrapped optional string when accessing their wrapped value as an explicit
You can think of an implicitly unwrapped optional as giving
permission for the optional to be force-unwrapped if needed. When you use an implicitly unwrapped optional value, Swift first tries to use it as an ordinary optional value; if it can’t be used as an optional, Swift force-unwraps the value. In the code above, the optional value
If an implicitly unwrapped optional is You can check whether an implicitly
unwrapped optional is
You can also use an implicitly unwrapped optional with optional binding, to check and unwrap its value in a single statement:
Note Don’t use an implicitly unwrapped optional when there’s a possibility of a variable becoming Error Handling¶You use error handling to respond to error conditions your program may encounter during execution. In contrast to optionals, which can use the presence or absence of a value to communicate success or failure of a function, error handling allows you to determine the underlying cause of failure, and, if necessary, propagate the error to another part of your program. When a function encounters an error condition, it throws an error. That function’s caller can then catch the error and respond appropriately.
A function indicates that it can throw an error by including the Swift automatically propagates errors out of their current scope until they’re handled by a
A Here’s an example of how error handling can be used to respond to different error conditions:
In this example, the If no error is thrown, the Throwing, catching, and propagating errors is covered in greater detail in Error Handling. Assertions and Preconditions¶Assertions and preconditions are checks that happen at runtime. You use them
to make sure an essential condition is satisfied before executing any further code. If the Boolean condition in the assertion or precondition evaluates to You use assertions and preconditions to express the assumptions you make and the expectations you have while coding, so you can include them as part of your code. Assertions help you find mistakes and incorrect assumptions during development, and preconditions help you detect issues in production. In addition to verifying your expectations at runtime, assertions and preconditions also become a useful form of documentation within the code. Unlike the error conditions discussed in Error Handling above, assertions and preconditions aren’t used for recoverable or expected errors. Because a failed assertion or precondition indicates an invalid program state, there’s no way to catch a failed assertion. Using assertions and preconditions isn’t a substitute for designing your code in such a way that invalid conditions are unlikely to arise. However, using them to enforce valid data and state causes your app to terminate more predictably if an invalid state occurs, and helps make the problem easier to debug. Stopping execution as soon as an invalid state is detected also helps limit the damage caused by that invalid state. The difference between assertions and preconditions is in when they’re checked: Assertions are checked only in debug builds, but preconditions are checked in both debug and production builds. In production builds, the condition inside an assertion isn’t evaluated. This means you can use as many assertions as you want during your development process, without impacting performance in production. Debugging with Assertions¶You write an assertion by calling the
In this example, code execution continues if You can omit the assertion message—for example, when it would just repeat the condition as prose.
If the code already checks the condition, you use the
Enforcing Preconditions¶Use a precondition whenever a condition has the potential to be false, but must definitely be true for your code to continue execution. For example, use a precondition to check that a subscript isn’t out of bounds, or to check that a function has been passed a valid value. You write a precondition by calling the
You can also call the Note If you compile in unchecked mode ( You can use the What is the name of the two digit number or digit alpha character that is located after the CPT code number and provides more detail about the code?Modifiers. The use of modifiers is an important component to coding and billing for services. A modifier is a two-digit character (numeric, alpha numeric, or alpha) designed to provide additional information needed to process a claim or increase or decrease reimbursement.
What does resequenced code mean?A resequenced code comes about when a new code is added to a family of codes but a sequential number is unavailable. A second exception to numerical code order involves evaluation and management (E/M) codes.
Which symbol in the CPT indicates that codes are out of numeric sequence?¯ The symbol (#) indicates a code that is out of numerical sequence. ¯ The star symbol ( ) indicates a telemedicine code. ¯ The circle and arrow symbol ( ) is a reference to CPT Assistant, Clinical Examples in Radiology, and CPT Changes. Code 20205 should be read as “Biopsy, muscle; deep.”
What type of code ends with 99?Test Review for Chapter 8. |