21 points that have to be known between Swift and OC

Source: Internet
Author: User

Since the June WWDC Congress by Apple's great God Chris Lattner to us for the first time to show that Swift has been more than half a year, although the vast majority of software companies in the code is still not seen the slightest shadow of swift, but the way Daniel has already had a variety of swift excavation. I'll summarize my views. Mainly for the curious OC siege Lion who hasn't studied swift yet.

If you do not see this article in the blog Park of Dong Platinum, please click to view the original

Between Swfit and OC.Contact

A semicolon is not required at the end of a 1.swift sentence, unless you want to write three lines of code in a row with semicolons separated.

2.swift do not write the main function , the program by default from top to bottom execution

3.swift does not divide. h and. m files , one class only. Swift one file

4.swift does not have the concept of address

5.swift data types are automatically judged , only variable var and constant let are distinguished

6. The forced conversion format is reversed OC Strong turn: (int) A swift strong turn: Int (a)

7. Integer data types can get maximum and minimum values through. Min and. Max

8. Define the type of alias syntax change oc:typedef int MyInt Swift:typealias MyInt = int

the modulo-remove operator for 9.swift supports decimals . such as 5%1.5 = 0.5

10. With regard to the more restrictive bool type , Swift is no longer OC 0 is true, but True is False is true

11. In connection with the 10th, theassignment operator for Swift has no return value . Prevent misuse of "=" and "= ="

12.swift can be many-to-many assignments . Let (x, y) = (on)

13.swift must have {} in the Loop statement , even if only one line of code must be added

The 14.swift switch statement can be followed only by integers, now with various data types , such as floating-point strings, and there is no need to write break,

If you don't want an effect that doesn't break, and you want to do it later, write the keyword Fallthrough (note: You can't define a constant variable after Fallthrough)

And then there are someSwift ExclusiveThe nature of the1.swift Exclusive Range operator

A...b means [A, b] such as 3 ... 5 is the range take 3,4,5

A.. <b means [A, b] if 3,5 is the range 3,4

Can be written directly in the for-in loop, or if judged as for x in 0...5 {}

2.swift Exclusive Overflow operator

By default, when you assign an integer constant or variable to a large number that it cannot host, Swift will not let you do so, and it will give an error. In this way, it is safe to operate too large or too small a number.

var potentialoverflow = int16.max//Potentialoverflow equals 32767, which is the largest integer Int16 can host Potentialoverflow + = 1//  error

Of course, you intentionally truncate the valid bits at overflow time, and you can use overflow operations instead of error handling. Swfit provides 5 overflow operators at the beginning of the & symbol for integer calculations.

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

Here's an example of an overflow.

var willoverflow = uint8.max//willoverflow equals UInt8 maximum integer 255willOverflow = willoverflow &+ 1//this time willoverflow equals 0

The general principle is to enter a extremes meet

3.swift unique tuple type

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

The tuple name is point, which has two elements x and Y. Kind of like a struct, but not.

The value assigned to the x that you want to take out is Point.x = 30 or point.0 = (Note: Many elements inside a tuple can be thought of as an array subscript)

The name of the inner element can be omitted var point = (15,20.2) But in this case, you can only use point.0 = 30 for the value, because there is no element name.

You can also explicitly specify the type of each element within a tuple, if that 20.2 I don't want a double type I want to be a float type. can var point = (int,string) = (15,20.2)

Note: Tuple names and types cannot coexist , such as when you specify a type, you can no longer specify a name, var point = (int,string) = (x:15,y:20.2)

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

You can also omit unwanted elements such as var point = (_, 20.2) at initialization time.

4. When using a tuple type in a switch statement, you can also add a filter condition with a syntax similar to SQL statements
Switch point{case let (x, y) where x== y:println ("x equals y values"), etc... }

5. External parameter names for functions

The original format of the function is this (the arrow is followed by the return value) Func Sum (num1:int,num2:int)-->int{}

Sum (20,20) when called

The addition of the external parameter name is more readable when the method call is written in front of the original parameter name and must be written when called .

That is, the Func Sum (numone num1:int,numtwo num2:int)-->int{}

Write Sum (numone:20,numbertwo:20) upon invocation

If you find this a bit cumbersome, you can let the external parameter name and the internal parameter name

Is the Func Sum (#num1: int, #num2: int)-->int{}

Write Sum (num1:20,num2:20) upon invocation

6. Default parameter values for functions

Func addstudent (name:string,age:int =)-->string{}

Set the default age to 20 so you just need to write a name when you call it.

Addstudent ("James")

Note that the default parameter value is used, and the system automatically generates an external parameter name.

If you want to change your name, you'll write an external parameter name, addstudent ("James", Age:18).

7. Input and output parameters of the function

Use InOut instead of Var when declaring a function so that you can later modify the outside value in the function to resemble the C language's incoming pointer

Func Change (inout num:int) {num = 10}var a = 20change (&a)  

The results are 10.

(Note: The syntax for the default function value cannot be used to write input and output parameters.)

Implement multiple return values with input and output parameters

Func Sumandminus (num1:int,num2:int,inout sum:int,inout minus:int) {sum = Num1 + num2 minus = Num1-num2}var sum = 0, MI NUS =0sumandminus (20,5,&sum,&minus)

Where to write not good welcome to add. If you do not see this article in the blog Park of Dong Platinum, please click to view the original

21 points that have to be known between Swift and OC

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.