As an interoperable language with Objective-C, Swift also has some of the same types and features as C, and provides a way to mix programming with common C code constructs if your code needs it.
Basic data types
Swift provides some basic data types equivalent to C primitives such as char, int, float, and double. However, these Swift core primitives are not implicitly converted to each other, such as Int. Therefore, use these types only when your code explicitly requires them, whereas ints can be used whenever you want.
C type | Swift type |
---|---|
bool | CBool |
char, signed char | CChar |
unsigned char | CUnsignedChar |
short | CShort |
unsigned short | CUnsignedShort |
int | CInt |
unsigned int | CUnsignedInt |
long | CLong |
unsigned long | CUnsignedLong |
long long | CLongLong |
unsigned long long | CUnsignedLongLong |
wchar_t | CWideChar |
char16_t | CChar16 |
char32_t | CChar32 |
float | CFloat |
double | CDouble |
The enumeration
As a developer, it is particularly important to have a learning atmosphere and a communication circle. Here is my iOS developer official account:Programming large xin, whether you are small white or big cattle are welcome to enter, let us progress together, common development! (The group will provide some free study books collected by the group owner and hundreds of interview questions and answers documents for free!)
Swift introduces any C-style enumerated types marked with the macro NS_ENUM. This means that whether enumerated values are defined in the system framework or in custom code, their prefix names will be truncated when they are imported into Swift. For example, look at this Objective-C enumeration:
1. //Objective-C
2. typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
3. UITableViewCellStyleDefault,
4. UITableViewCellStyleValue1,
5. UITableViewCellStyleValue2,
6. UITableViewCellStyleSubtitle
7. };
Copy the code
Do this in Swift:
1. //Swift
2. enum UITableViewCellStyle: Int {
3. case Default
4. case Value1
5. case Value2
6. case Subtitle
7. }
Copy the code
When you need to point to an enumeration value, use the dot (.) Enumeration names at the beginning:
1. //Swift
2. let cellStyle: UITableViewCellStyle = .Default
Copy the code
Swift also introduced macro options labeled NS_OPTIONS. And the behavior of the option is similar to the introduction of enumeration, option can also support some operations, such as &, | and ~. In Objective-C, you use an empty option to flag constant zero (0). In Swift, using nil means there are no options.
Pointer to the
Swift avoids giving you direct access to Pointers as much as possible. However, Swift also provides you with multiple pointer types when you need to manipulate memory directly. The following table uses Type as a placeholder Type name to represent the mapping of the syntax. For parameters, use the following mapping:
C syntax | Swift syntax |
---|---|
const void * | CConstVoidPointer |
void * | CMutableVoidPointer |
const Type * | CConstPointer |
Type * | CMutablePointer |
For multi-level Pointers to return types, variables, and parameter types, use the following mapping:
C syntax | Swift syntax |
---|---|
void * | COpaquePointer |
Type * | UnsafePointer |
For class types, use the following mapping:
C syntax | Swift syntax |
---|---|
Type * const * | CConstPointer |
Type * __strong * | CMutablePointer |
Type ** | AutoreleasingUnsafePointer |
C Mutable pointer
When a function is declared to accept a CMutablePointer argument, the function can take any of the following types as arguments:
- Nil, passed as a null pointer
- A value of type CMutablePointer
- An operand is an input-output expression of an lvalue of Type Type, passed as the memory address for that lvalue
- An input-output Type[] value is passed as a starting pointer to an array, and its lifetime is extended during this call
If you declare a function like this:
1. //Swift 2. func takesAMutablePointer(x: CMutablePointer<Float>) { /*... * /}Copy the code
Then you can call this function in any of the following ways:
Var x: Float = 0.0 3. Var p: CMutablePointer<Float> = nil 4. Var a: Float [] = [1.0, 2.0, 3.0] 6. takesAMutablePointer(nil) 7. takesAMutablePointer(p) 8. takesAMutablePointer(&x) 9. takesAMutablePointer(&a)Copy the code
When a function is declared to use a CMutableVoidPointer argument, the function accepts any Type operand of a Type similar to CMutablePointer.
If you define a function like this:
1. //Swift
2. func takesAMutableVoidPointer(x: CMutableVoidPointer) { /* ... */ }
Copy the code
Then you can call this function in any of the following ways:
//Swift var x: Float = 0.0, y: Int = 0 var p: CMutablePointer<Float> = nil, q: CMutablePointer<Int> = nil var a: Float = 0.0, y: Int = 0 var p: CMutablePointer<Float> = nil Float[] = [1.0, 2.0, 3.0], b: Int = [1, 2, 3] takesAMutableVoidPointer(nil) takesAMutableVoidPointer(p) takesAMutableVoidPointer(q) takesAMutableVoidPointer(&x) takesAMutableVoidPointer(&y) takesAMutableVoidPointer(&a) takesAMutableVoidPointer(&b)Copy the code
C pointer often
When a function is declared to take a CConstPointer argument, the function can take any of the following types as arguments:
- Nil, passed as a null pointer
- A CMutablePointer, CMutableVoidPointer, CConstPointer, CConstVoidPointer, Or where necessary into CConstPointer AutoreleasingUnsafePointer values
- An operand is an input-output expression of an lvalue of Type Type, passed as the memory address for that lvalue
- A Type[] array value, passed as a starting pointer to an array, and its life is extended during this call
If you define a function like this:
1. //Swift 2. func takesAConstPointer(x: CConstPointer<Float>) { /*... * /}Copy the code
Then you can call this function in any of the following ways:
Var x: Float = 0.0 3. Var p: Float = 0.0 CConstPointer<Float> = nil 5. takesAConstPointer(nil) 6. takesAConstPointer(p) 7. takesAConstPointer(&x) 8. TakesAConstPointer ([1.0, 2.0, 3.0])Copy the code
When a function is declared to use a CConstVoidPointer argument, the function accepts any Type operand of a Type similar to CConstPointer. If you define a function like this:
- / / Swift   
- func takesAConstVoidPointer(x: CConstVoidPointer) { /* … */ }
Then you can call this function in any of the following ways:
//Swift 2. var x: Float = 0.0, y: Int = 0 3. var p: CConstPointer<Float> = nil, q: CConstPointer<Int> = nil 5. takesAConstVoidPointer(nil) 6. takesAConstVoidPointer(p) 7. takesAConstVoidPointer(q) 8. TakesAConstVoidPointer (&x) 9.TakesaconstVoidPointer (&y) 10.TakesaconstVoidPointer ([1.0, 2.0, 3]) 11. TakesAConstVoidPointer ([1, 2, 3])Copy the code
Automatically release the unsafe pointer
When a function is declared to accept AutoreleasingUnsafePointer parameters, this function can be accepted as a parameter to any of the following types:
- Nil, passed as a null pointer
- A AutoreleasingUnsafePointer value
- An input-output expression whose operands are raw, copied into a temporary buffer with no owner, whose address is passed to the call, and returned when the values in the buffer are loaded, saved, and reassigned to the operands.
Note: This list does not contain arrays.
If you define a function like this:
1. //Swift
2. func takesAnAutoreleasingPointer(x: AutoreleasingUnsafePointer<NSDate?>) { /* ... */ }
Copy the code
Then you can call this function in any of the following ways:
1. //Swift 2. var x: NSDate? = nil 3. var p: AutoreleasingUnsafePointer<NSDate? 4 > = nil      5. TakesAnAutoreleasingPointer (nil) 6. TakesAnAutoreleasingPointer (p) 7. takesAnAutoreleasingPointer(&x)Copy the code
Note: C function Pointers are not introduced by Swift.
Global constants
Global constants defined in C and Objective-C source files are automatically imported by Swift compilation and used as global constants for Swift.
Preprocessing instruction
The Swift compiler does not contain a preprocessor. Instead, it takes full advantage of compile-time attributes, build configurations, and language features to accomplish the same functionality. Therefore, Swift does not introduce preprocessing instructions.
Simple macros
In C and Objective-C, you typically use the #define directive to define a macro constant. In Swift, you can use global constants instead. For example, a global definition #define FADE_ANIMATION_DURATION 0.35 can be better expressed in Swift using let FADE_ANIMATION_DURATION = 0.35. Because simple macros used to define constants are mapped directly to Swift global quantities, the Swift compiler automatically introduces simple macros defined in C or Objective-C source files.
Complex macro
Complex macros used in C and Objective-C have no Swift equivalent. Complex macros are macros that do not define constants, but macros that contain parentheses (), functions. You use complex macros in C and Objective-C to avoid type checking limitations and rework of the same code. However, macros can also cause bugs and refactoring difficulties. In Swift you can use functions and generics directly to achieve the same effect. Therefore, complex macros defined in C and Objective-C source files are not usable in Swift.
Compile the configuration
Swift code and Objective-C code are conditionally compiled differently. Swift code can be compiled conditionally based on the evaluation configuration of the generated configuration. The build configuration includes true and false literals, command line flags, and platform test functions in the table below. You can specify the command line flag with -d < # Flag# >.
function | Effective parameters |
---|---|
os() | OSX, iOS |
arch() | x86_64, arm, arm64, i386 |
Note: The build configuration for ARCH (ARM) does not return true for 64-bit ARM devices. The build configuration for ARCH (I386) returns true when code is running on a 32-bit ios emulator.
A simple conditional compilation requires the following code format:
1. #if build configuration
2. statements
3. #else
4. statements
5. #endif
Copy the code
A statement made with zero or more valid Swift statements can include expressions, statements, and control flow statements. You can add additional build configuration requirements, conditional compilation instructions use && and | | operators and! Operator to add conditional control blocks with # elseif:
1. #if build configuration && ! build configuration 2. statements 3. #elseif build configuration 4. statements 5. #else 6. statements 7. #endifCopy the code
In contrast to conditional compilation in the C language compiler, Swift conditional compilation statements must be completely self-contained and syntactically valid blocks of code. This is because Swift code is all syntactically checked even if it is not compiled.