iOS Development Swift-(c) strings and data types

Source: Internet
Author: User

iOS Development Swift-(c) strings and data types

One, the string

String is data of type string, with double quotation mark "" Wrap text content Let website = "Http://www.wendingding.com"

1. Common Operations for strings

(1) Use Plus + to do string concatenation

Let scheme = "/http"

Let path = "www.wendingding.com"

Let website = scheme + path

Website's content is "http://www.wendingding.com"

(2) Use backslashes \ and parentheses () to interpolate the string (insert the constant \ variable into the string)

Let hand = 2

var age = 20

Let str = "I am (age) year old, have \ (hand) only Hand"

The content of STR is "I am 20 years old and have 2 hands."

Note: Swift is a type-safe language, and different data types are not additive. (Requires cast type)

2. Print output

Swift provides 2 print output functions

PRINTLN: The output will be wrapped automatically after the content

Print: Compared to println, there is less of a line wrapping function

Ii. Types of data

The data types commonly used in 1.Swift are

Int, Float, Double, Bool, Character, String

Array, Dictionary, tuple type (tuple), optional type (Optional)

Note: The first letter of the data type is uppercase

2. Specifying the data type of the variable \ constant

Add a colon (:) and a type name after the constant \ variable name

Let Age:int = 10

The code above indicates that a constant age of type int is defined, with an initial value of 10

Tip: In general, it is not necessary to explicitly specify the type of variable \ constant. If you assign an initial value when declaring a constant \ variable, swift can infer the type of the constant \ variable automatically

For example: let-age = Swift infers that age is an int because 20 is an integer

Iii. Initialization of variables

Swift requirements variables must be initialized before they are used

Note: The following code is incorrect

var a:int

Let C = A + 10

Description

1th line of code: Swift does not assign an initial value to variable A, and a does not initialize

The 2nd line of code will error

The following code is correct

var a:int = 20

Let C = A + 10

Four, integer

1. Classification of integers

Integers are divided into 2 types

Signed (Signed): positive, negative, zero

unsigned (unsigned): positive, Zero

Swift provides 8, 16, 32, 64-bit signed and unsigned integers, such as

Uint8:8-bit unsigned integral type

INT32:32 bit signed integral type

Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64

2. Maximum Value

You can get the min and Max values for a type by using the Min and Max properties

Let MinValue = uint8.min//UInt8 type min equals 0

Let MaxValue = uint8.max//UInt8 type Max equals 255

3.Int and UINT

Swift also provides special signed integer type int and unsigned integer type UINT

Description: The int\uint is the same length as the current system platform

On 32-bit system platforms, the length of int and uint is 32 bits

On 64-bit system platforms, the length of int and UINT is 64 bits

Value range of int on 32-bit system platform: 2147483648 ~ 2147483647

Suggestions

When defining variables, don't always consider the question of unsigned, data length

Use int as much as possible to ensure the simplicity and reusability of the Code

4. Storage Range

Each data type has its own storage range, such as

Int8 Storage Range is: –128 ~ 127

The UInt8 storage range is: 0 ~ 255

If the value exceeds the storage range, the compiler will directly error

NOTE: The following statement will directly error

Let num1:uint8 =-1

Let num2:int8 = Int8.max + 1

Description

1th Line Code Error Reason: UInt8 cannot store negative numbers

The 2nd line of code error causes: Int8 can store the maximum value is Int8.max

5. Representation of integers

4 Representations of integers

(1) Decimal number: no prefix

Let I1 = 10//10

(2) Binary number: prefixed with 0b

Let i2 = 0b1010//10

(3) Octal number: prefixed with 0o

Let i3 = 0o12//10

(4) Hexadecimal number: prefixed with 0x

Let I4 = 0xA//10

Five, floating point number

1. Floating-point number description

Floating-point numbers are decimals. Swift provides two types of floating-point numbers

double:64 bit floating point number, used when floating-point values are very large or need to be very precise

float:32 bit floating point number used when floating-point values do not need to use a double

Degree of accuracy

Double: At least 15 decimal places

Float: At least 6 decimal places

Note: If the type is not explicitly specified, the floating-point number defaults to double type

Let num = 0.14//num is a constant of type double

2. Representation of floating-point numbers

Floating-point numbers can be represented in decimal and hexadecimal 2.

(1) decimal (no prefix)

1) No index: let D1 = 12.5

2) Index: let D2 = 0.125e2

0.125e2 = = 0.125 * 10²

Men = = M * 10 N-Th Square

(2) hexadecimal (prefixed with 0x and must have an exponent)

Let D3 = 0xc.8p0

0xc.8p0 = = 0xc.8 * 2º== 12.5 * 1

0xMpN = = 0xM * 2 of the n-th square

Let D3 = 0xc.8p1

0XC.8P1 = = 0xc.8 * 2¹== 12.5 * 2 = 25.0

3. Number format

Numbers can add extra formatting to make them easier to read

(1) Additional 00 can be added.

Let money = 001999//1999

Let Money2 = 001999.000//1999.0

(2) Additional underline can be added to enhance readability

Let OneMillion1 = 1_000_000//1000000

Let OneMillion2 = 100_0000//1000000

Let overonemillion = 1_000_000.000_001//1000000.000001

Description: Added an additional 00 and underline _, and does not affect the original value size

VI. Type conversion

Two values of different types are not directly calculated.

Example 1:

NOTE: The following statement is incorrect

Let num1:uint8 = 10;

Let num2:int = 20;

Let Sum:int = Num1 + num2

3rd Guild Error Description:

Error reason: NUM1 is UInt8 type, num2 is type int, type is different, cannot add

Solution: Convert NUM1 to int type to add to num2

The following statement is correct

Let Sum:int = Int (NUM1) + num2

Example 2:

NOTE: The following statement is incorrect

Let NUM1 = 3//NUM1 is int type

Let num2 = 0.14//num2 is a double type

Let sum = num1 + num2

3rd Line Error Description:

Cause of Error: NUM1 is type int, num2 is type double, type is different, cannot add

Solution: Convert NUM1 to double type to add to num2

The following statement is correct

Let sum = Double (NUM1) + num2

Attention:

The following is the correct wording

Let sum = 3 + 0.14

The compiler will automatically infer that sum is a double type after 3 and 0.14 are added to result 3.14, etc.

Vii. type aliases

You can use the Typealias keyword to define the alias of the type, similar to the typedef of the C language

Typealias MyInt = INT//gives an int type an alias called MyInt

Where the original type name can be used, where the alias can be used

Declaring variable \ constant type

Let Num:myint = 20

Get the maximum value of a type

Let MinValue = Myint.min

Type conversions

Let num = MyInt (3.14)//3

  

iOS Development Swift-(c) strings and data types

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.