Constants and variables
Declare constants and variables
swift
Let is used for constant declarations and var is used for variable declarations
Let must set a value (or type annotation) when it is declared, and the constant cannot be changed after it is declared. Var must also set a value (or type annotation) when it is declared, but it can be changed later. Only values of the same type can be changed when it is changed
Changing a constant value or setting a value of another type to a declared variable is an error
OC
Constant declarations use const, variable declarations use ID
const
Note that the value to the right of const cannot be changed.
Const NSString * STR = @”111″; * STR cannot be changed. STR can be changed. In this case, use STR = @”222″ to change the value of STR.
NSString * const str2 = @”11″; Str2 cannot be changed. *str2 can be changed. If str2 = @”222″, an error will be reported
id
Id variables do not have to be declared immediately, can be changed later, and can subsequently accept different types of values
Type annotation
swift
Swift can carry out type annotation when declaring constants and variables. After the type annotation of constants, the initial value can be set later, but the initial value cannot be changed once set.
OC
OC has a similar type annotation for declaring constants and variables, such as constants and variables of type int
const int maxIndex = 10; Int index = 0; // variable // declaration string const NSString * STR = @"aaa"; NSString *name = @"LiLei";Copy the code
Output constants or variables
swift
Swift uses print to print constants or variables, such as inedx:
- If you want to print a constant or variable directly, you can print it directly
print(index)
To print - If you want to print with other variables, you can list them in parentheses separated by commas
print(index,index2,index3)
- If you want to print it with other strings, you can
Print (" this is the character: ",index)
orPrint (" This is character: \(index)")
That is, the string to be printed is passed in as an argument or a variable\ ()
The parcel
OC
There are two ways to print printf and NSLog in OC. Printf mainly prints C values, while OC mainly uses NSLog. NSLog is used as follows: If you want to print the value of a variable, you need to know the class of the variable, and if it is a string, you use %@, %d is used for int, %f is used for float, and multiple arguments are used similarly
In contrast to Switft and OC in variable printing, Swift is convenient without specifying specific placeholders
annotation
Note the use of swift and OC is basically the same.
- Single line comment, single line comment is used
// The contents of the comment
- Multi-line comments, multi-line comments are used
/* The contents of the comment */
The difference is that Swift allows multi-line comments to be nested, whereas OC does not
/* This is a /* multi-line comment */ see clearly */Copy the code
A semicolon
A semicolon is optional at the end of a swift line, but if two lines of code are on one line, the first line must be followed by a semicolon.
A semicolon must end each line in the OC.
The integer
swift
In Swift, Int and UInt are commonly used to declare signed and unsigned integers. Using Int and UInt automatically limits the value range based on the current platform, or Int = Int32 if it is 32 bits
oc
NSInteger and NSUInteger are commonly used in OC to declare signed and unsigned integers, and they also automatically limit the value range based on the current platform.
Floating point Numbers
swift
Double: a 64-bit floating-point number with at least 15 digits of accuracy
Float: a 32-bit floating point number with 6 digits of precision
oc
Double: a 64-bit floating-point number with at least 15 digits of accuracy
Float: a 32-bit floating point number with 6 digits of precision
Type safety and type inference
Swift is a type-safe language, which is a big difference from OC.
- Type safety: Type checking is done at compile time, and if the type does not match, it is flagged as an error.
- Type inference: When no type is specified, Swift does type inference based on the initial value.
Numeric type conversion
swift
When different types are put together for an arithmetic operation, the different types of data are explicitly converted. For example:
Let a = 3 let b = 1.5 let c = Double(a) + b // c = 4.5Copy the code
So c is of type Double
Can also:
Let b = 1.5 let c = a + Int(b) // c = 4Copy the code
Note that when floating point numbers are converted to integers, the data will be truncated, such as 3.9 -3, -3.9 -3
OC
When different types are put together to perform arithmetic operations in oc, there is no need for explicit data conversion because the receiver of the underlying data type must be of the specified type, so there is a standard for converting data after the specified type.
Double d1 = 1.6; int i1 = 3; int temp = d1 + i1; printf("%d \n",temp); // Print the result: 4Copy the code
1. Different types of operations do not explicitly convert data. They infer whether to convert data based on the type of the result. When a floating-point number is converted to an integer, the data is truncated, as in swift
Boolean value
swift
The bool values in swift represent true and false
OC
The bool value in OC indicates YES and NO
tuples
swift
A tuple is an array that can hold many different types of data.
- Tuples can only be constants and cannot be changed after they are defined, nor can their internal values be changed
- Tuples allow constant or variable names to be defined for elements inside a tuple
Let temp = (11,"234") let (index1, _) = temp print(index1Copy the code
- Tuples support access to elements using subscripts
Print (temp.0) // Print: 11Copy the code
- You can specify a name when you define a tuple
Let temp = (index1:11, STR :"234") print(index1) print(temp. Index1) print(tempCopy the code
OC
Oc has no concept of tuples, but tuples are somewhat similar to structures in C
optional
swift
Optional means that the variable may have a value or may not have one:
var temp: Int?
Copy the code
You just add a “? “after the type annotation.
Note: Optional means that the value is either int or nothing. If there is no value, temp = nil
You can add after optional variables in Swift! Add an exclamation mark to indicate “I’ve determined that this option has a value, get the value out of it.” If there is no value, it will cause a runtime error, so it’s best to use the if statement before using the option
OC
Oc has a similar way of writing it so that you can optionally define an attribute by adding _Nullable, which means that the attribute may or may not have a value,
nil
swift
Nil in Swift can only be assigned to optional variables, non-optional variables or constants cannot be assigned to nil; nil is a special type with missing values
oc
Nil in OC represents a pointer to an object that does not exist
assertions
swift
Assertions are valid only for debugging. 2. In assertions, if the condition is true, the system continues to run
Var a = 3 Assert (a>0," A >0 ")Copy the code
The use of assertions in OC is consistent with this
A prerequisite for
swift
Prerequisites are only available in Swift, and they differ from Assert in that they can be used in a production environment and are used in the same way as Assert
precondition(index > 0, "Index must be greater than zero.")
Copy the code