21 points between swift and OC must be known, and swiftoc cannot be 21 points

Source: Internet
Author: User

21 points between swift and OC must be known, and swiftoc cannot be 21 points

Chris Lattner, Apple's great god, presented swift to us for the first time at the WWDC conference in June. It has been more than half a year since then. Although most software company code has not seen swift, however, various great bulls have already begun to explore swift. I will summarize my views. It is mainly for OC siege Lions who have not carefully studied swift.

If you did not see this article in Dong Boran's blog Park, click to view the original article.

Relationship between swfit and OC

1. a semicolon is not required at the end of a swift sentence, unless you want to write three lines of code in a row, it will be separated by extra points.

 

2. Do not write the main function in swift. The program is executed from top to bottom by default.

 

3. swift does not include. h and. m files. A class only has. swift files.

 

4. swift does not have an address.

 

5. swift data types are automatically determined. Only variables var and constant let are distinguished.

 

6. Forced conversion format reversed OC strong conversion: (int) a Swift strong conversion: int ()

 

7. The integer data type can be obtained through. min and. max to obtain the maximum and minimum values.

 

8. Syntax change of alias for definition type OC: typedef int MyInt Swift: typealias MyInt = int

 

9. The modulo remainder operator of swift supports decimal places. For example, 5% 1.5 = 0.5

 

10. For more strict BOOL types, Swift is no longer true if OC is not 0, but false if true.

 

11. In connection with, swift's value assignment operator does not return values. Prevents misuse of "=" and "="

 

12. swift can assign multiple to multiple values. Let (x, y) = (1, 2)

 

13. {} must be added to the swift loop statement, even if there is only one line of code.

 

14. The switch statement of swift can only be followed by integers. Now it can be followed by various data types, such as floating-point strings, without the need to write break,

If you do not want the effect without a break, that is, if you want to execute all the subsequent tasks, write the keyword fallthrough (Note: constant variables cannot be defined after fallthrough)

Next, there are some unique swift properties. 1. The range operators unique to swift

A... B indicates [a, B]. For example, 3... 5 indicates that the range is 3, 4, 5.

A .. <B Indicates [a, B). For example, 3, 5 indicates that the value ranges from 3 to 4.

It can be written directly in the for-in loop, or in the if judgment, such as for x in 0... 5 {}

2. Exclusive overflow operators of swift

By default, When you assign an integer constant or variable to a large number that it cannot carry, Swift will not let you do this and it will report an error. In this way, the operation is safe when it is too large or too small.

Var potentialOverflow = Int16.max // potentialOverflow equals 32767, which is the maximum integer potentialOverflow + = 1 // Error

Of course, you intend to truncate the valid bits when overflow occurs. You can use overflow operations instead of error processing. Swfit provides five overflow operators starting with & symbol for integer calculation.

& +, &-, & *, &/, & %

Here is an example of overflow.

Var willOverflow = UInt8.max // willOverflow equals the maximum integer of UInt8 255 willOverflow = willOverflow & + 1 // willOverflow equals 0 at this time

The general principle is that it is absolutely difficult to get into a thing.

3. Unique swift tuples

Var point = (x: 15,y: 20.2)

That is, the name of the tuples is point, which contains two elements x and y. Similar to struct, but not

If you want to obtain the value of x, the value assignment is point. x = 30 or point.0 = 30 (Note: many elements in the tuples can be considered as having an array subscript)

You can omit the internal element name var point = (15, 20.2). However, you can only use the point.0 = 30 Method to retrieve the value, because there is no element name.

You can also explicitly specify the type of each element in the element group. If the value 20.2 does not require the double type, I want to use the float type. Var point = (Int, String) = (15, 20.2)

Note: The names and types of tuples cannot coexist. For example, if you specify a type, you cannot specify the name var point = (Int, String) = (x: 15, y: 20.2)

If you want to print it, write println (point) to print it (10, 20.2)

During initialization, you can also use underscores to omit unnecessary elements such as var point = (_, 20.2 );

4. When using the tuples in the switch statement, you can also use the syntax similar to the SQL statement to add filter conditions.
Switch point {case let (x, y) where x = y: println ("x and y values are equal"); and so on ......}

 

5. External Parameter Name of the Function

The original function format is as follows (the return value is behind the arrow) func Sum (num1: Int, num2: Int) --> Int {}

The value is Sum (20, 20)

When an External Parameter Name is added, the method is more readable and must be written before the original parameter name.

That is, func Sum (numone num1: Int, numtwo num2: Int) --> Int {}

Write Sum (numone: 20, numbertwo: 20)

If this is troublesome, you can make the External Parameter Name the same as the internal parameter name.

Is func Sum (# num1: Int, # num2: Int) --> Int {}

Write Sum (num1: 20, num2: 20)

6. Default function parameter value

Func addStudent (name: string, age: Int = 20) --> string {}

If the default age is set to 20, you only need to write a name for the next call.

AddStudent ("james ")

Note that an external parameter name is automatically generated when the default parameter value is used.

To change the name, you need to write an external parameter name, that is, addStudent ("james", age: 18)

7. Input and Output Parameters of the Function

In the function declaration, replace var WITH inout. In this way, you can modify the value outside the function, which is similar to the input pointer of C language.

func change (inout num:Int) {  num = 10}var a = 20change(&a)  

The result is 10.

(Note: If you write an input/output parameter, you cannot use the default function Value Syntax)

Use input and output parameters to implement multiple return values.

func SumAndMinus(num1:Int,num2:Int,inout sum:Int,inout minus:Int){  sum = num1 + num2  minus = num1 - num2}var sum = 0 ,minus =0SumAndMinus(20,5,&sum,&minus)

Where is it hard to write. If you did not see this article in Dong Boran's blog Park, click to view the original article.

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.