[Swift learning] Swift programming journey (2): swift learning programming journey

Source: Internet
Author: User

[Swift learning] Swift programming journey (2): swift learning programming journey

This section describes some basic knowledge.

Swift provides its own version types. The following describes several simple types.

Int integer, Double, float, String, Boolean

 

It also provides three main powerful Collection types: set, array, and dictionary.

 

Like C, Swift stores the variable class and associates the value with the variable name.

 

  Swift advanced type [not in OC]

1. Swift also provides advanced types not available in oc, suchTuples(Tuple). tuples allow you to create or pass a set of data. For example, you can use a single Tuple to return multiple values when being the return value of a function.

2.OptionalTo process missing values. In swift, not only classes and objects can be nil, but any type can be nil.

 

Swift is a secure language, which means that this language helps you clean up the value type during your encoding. You expect to input a value of the String type, type security will prevent you from passing in an int value, and a compilation error will occur when you pass in.

 

The following describes several simple data types in swift.

 

I. integer

Swift provides signed and unsigned integers. These integers follow naming conventions similar to C. An 8-bit unsigned integer is of the uint8 type, and a 32-bit signed integer is of the Int32 type. You can obtain the maximum and minimum values of different integer data.

UInt8.min the minimum UInt8.max value of an 8-digit integer.

 

  Int

In most cases, you do not need to select an integer of a specific size in your code. SWIFT provides an additional integer, int, with the same length range as the current platform.
On a 32-bit platform, the size of int is the same as that of Int32.
On a 64-bit platform, int and int64 are of the same size.
Unless you need to work with an integer of a specific size, always use the int integer value in your code. This facilitates code consistency and interoperability. Even on 32-bit platforms, int can store any value between-2147483648 to 2147483647, an integer range large enough.

 

  UInt

It also provides an unsigned integer type, uint, with the same length range as the current platform.
On a 32-bit platform, the size of uint is the same as that of UInt32.
On a 64-bit platform, the uint is the same as the UInt64 size.

Note:
Do not use UInt unless you really need to store an unsigned integer with the same native character length as the current platform. In addition to this situation, it is best to use Int, even if the value you want to store is known to be non-negative. The unified use of Int can improve code reusability, avoid conversion between different types of values, and match the type estimation of numbers.

 

2. floating point value

Swift provides two types of signed floating point numbers

-DoubleIndicates a 64-bit floating point number. Use this type when you need to store large or high-precision floating point numbers.

-Float32-bit floating point number. This type can be used if the precision requirement is not high.

 

Type security and speculation

Swift is type-safe, so it performs type check (type checks) when compiling your code and marks the mismatched type as an error. This allows you to detect and fix errors as soon as possible during development. Type check can help you avoid errors. However, this does not mean that you need to explicitly specify the type every time you declare constants and variables. If you do not explicitly specify a type, Swift uses type inference to select the appropriate type. With the speculative type, the compiler can automatically deduce the type of the expression when compiling the code. The principle is very simple. You only need to check the value you assigned. Because of the speculative type, Swift seldom needs to declare the type compared with C or Objective-C. Although constants and variables need to be clearly typed, most of the work does not need to be done by yourself. When we speculate on the floating point type, Swift always chooses Double instead of Float.

 

  Numeric literal

Decimal number with no prefix
Binary Number, with the 0B prefix
Octal number, with the 0o prefix
Hexadecimal number, a 0x prefix

 

Numeric type conversion

The type must be explicitly specified for integer and floating-point conversions.

 

  Type alias

A type aliases is another name defined for an existing type. You can useTypealiasKeyword to define the type alias

typealias AudioSample = UInt16

 

 

Booleans Boolean Value

Swift has a basic Boolean Type called Bool. Boolean values are logical values because they can only be true or false. Swift has two Boolean constants: true and false:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.