Recently, I have read many tutorials and daishen’s blog during my study of Swift. Finally, I put my notes here for easy review

W3cschool -Swift tutorial, YungFan’s brief book (recommended to have a look, some knowledge summary is simply easy to understand)

First, basic knowledge

1. The difference between SWIFT and OC

  1. Semicolon: Swift does not require a semicolon at the end, except when several lines of code are joined on one line

  2. Space format: Swift pays great attention to format. A = 1 and b=2 must be in the same format on both sides of the equal sign

  3. Method call: oc [Person saveCall]; ; Person. SaveCall in Swift similar to oc point syntax

  4. Header files: Swift can reference methods and properties of other classes without importing header files

  5. Enumerated types: can be abbreviated, direct. Enumerated values

  6. Self: Accesses an attribute or method in the current class. You can omit self.

  7. Oc [[Person alloc] initWithName: XXX] -> Swift Person(name: XXX)

  8. #selector: at sign selector in OC, #selector in Swift

2. Type safety

Swift is a Type safe language. Var is used to declare a variable. Only the value of the corresponding type can be changed, but not the data type. Type checking is done at compile time, and errors are reported for mismatches.

Var a = 12 a = "hello" // This is an intCopy the code

3. Type inference

When we declare a constant with let or a variable with var, we can omit its type in the case of direct assignment. Swift uses type inference to select the appropriate type

Let a = 100 let b = 3.14 let c = "hello"Copy the code

Note: If a floating-point literal is not typed, Swift will automatically infer that it is of type Double:

Let a = 3.14159 print(type(of: a))//Double let b = 3 + 0.14159 print(type(of: b))//DoubleCopy the code

Type aliases

Same as oc usage, but the keyword is decorated with typeAlias

// declare typeAlias ABC = Int // call let a: ABC = 100Copy the code

Second, data type

1, the integer

  • Int (signed integer: positive, zero, negative)
  • UInt (unsigned integer: positive, zero)

Matters needing attention:

It is generally not necessary to set the specific Int8, Int16. Just write an Int, the length is OS dependent, like on a 32-bit system, write an Int just like Int32. UInt Int8, Int16, Int32, Int64 are signed integers of 8, 16, 32, and 64 bits, respectively. UInt8, UInt16, UInt32, UInt64 The value is an unsigned integer of 8, 16, 32 and 64 bits respectively.Copy the code

Integer range:

The min and Max values for each integer type can be accessed via the min and Max attributes: Let minValue = uint8.min // The minimum value is 0 and the value type is UInt8 let maxValue = uint8.max // the maximum value is 255 and the value type is UInt8Copy the code

2, floating point

  • Double represents a 64-bit floating point number.
  • Float represents a 32-bit floating point number.

Matters needing attention:

Double has an accuracy of at least 15 digits, while Float has an accuracy of only 6 digits. Which floating point type you use depends on the range of values your code needs to handle. Double is recommended when both types are possible.Copy the code

3. Boolean

Bool, two Boolean constants: true and false

4. String & character

  • String: String
  • Character: indicates a single Character

Code examples:

// let a:Int = 10 // let b :Int = 12 // Var c = 20 c = 25 let d:Float = 3.14 // Specify Float type let e = 3.15 // Default Double type var f = 3.16 // Default Double type f = 3.17Copy the code

3. Literals

A literal is a value such as a particular number, string, or Boolean that directly identifies its type and assigns a value to a variable. Such as:

Let aNumber = 3 // Integer literal let aString = "Hello" // string literal let aBool = true // Boolean literalCopy the code
  • Integer literals

An integer literal can be a decimal, binary, octal, or hexadecimal constant

// let decimalInteger = 17 // binary, prefix: 0b let binaryInteger = 0b10001 // octal, prefix: 0o let octalInteger = 0O21 // Hexadecimal prefix: 0x let hexadecimalInteger = 0x11Copy the code
  • Floating point literals

Composition: integer part, decimal point, decimal part, exponent part The default is base 10, can also be used in hexadecimal (prefix 0x)

Base 10 and exp

The exponent of a decimal number with exp is equal to the base multiplied by 10exp: 1.25e2 means 1.25 x 10^2, or 125.0. 1.25E-2 means 1.25 x 10^-2, or 0.0125Copy the code

Hexadecimal and exp indices

The hexadecimal number and exp exponent result is equal to the base number multiplied by 2exp: 0xFp2 means 15 x 2^2, or 60.0. 0xFP-2 means 15 x 2^-2, or 3.75.Copy the code

In 12.1875, for example

Let decimalDouble = 12.1875 let exponentDouble = 1.21875E1 let hexadecimalDouble = 0xc.3P0Copy the code
Note:
Literals of numeric types, Let paddedDouble = 000123.456 let oneMillion = 1_000_000 let justOverOneMillion = 1_000_000.000_000_1Copy the code
  • String literals

String literals cannot contain unescaped double quotes (“), unescaped backslashes (\), carriage returns, or newlines.

  • Boolean value literals

The default type for Boolean literals is Bool. Boolean literals have three values, which are reserved keys for Swift:

True indicates true. False indicates false. Nil means no value.Copy the code

Four, constant

1. Naming rules:

  • Let decorate
  • Start with a letter or underscore
  • Case sensitive, two variables
  • You can use simple Unicode characters such as Hanzi and emoticons
let a : Int = 10
let a = 10
let _a = 10
let view : UIView = UIView()
Copy the code

2. Note:

  • Let is immutable
  • The constant we modify with let is a class, and we can modify its properties
  • A constant cannot be modifiedPointers can no longer point to other objects.But you can get a pointer to an object and change its properties

Five, the variable

1. Naming rules:

  • Var decoration
  • Start with a letter or underscore
  • Case sensitive, two variables
  • You can use simple Unicode characters such as Hanzi and emoticons
Var _a = "hello" var b = "hello" var b = "hello" var Hello = "hello"Copy the code

2. Output:

Print (_a) print (b) print (you are) print (hello, _a, b) print (say \ "meet (_a)")Copy the code

3. Note:

  • We’re not gonna usevarThere is no need to refer to a class
  • For security purposes, it is recommended to define constants first and then change them to variables if necessary

Special operators

Here’s just one less common one:

  • = = =! = =: tests whether the runtime type of an instance matches its compile-time type
if someInstance.dynamicType === someInstance.self {
    print("The dynamic and static type of someInstance are the same")
} else {
    print("The dynamic and static type of someInstance are different")
}
Copy the code