Comparison between the basic syntax of Swift and C,

Source: Internet
Author: User
Tags array definition constant definition

Comparison between the basic syntax of Swift and C,
Background:

I accidentally read the basic syntax of Swift over the past two days. Now I feel that I have taken a note and left a trace ~

In general, Swift is a product of mixed frontend and backend languages ~~~

As A. NET camp person, a few people always prefer to deepen their understanding through comparison.

Therefore, we made a simple comparison list.

Below is a comparison of the basic syntax:

 

 

Swift

C # (more than 4.0)

Constant definition:

Let name = "cyq. data"

Const name = "cyq. data ";

Variable definition:

Type:

Var name = "cyq. data"

Var name: String = "cyq"

Var name = "cyq. data ";

String name = "cyq ";

Type conversion:

Var I = 3;

Var s: String = String (I)

Var s2: Strng = "\ (I )"

Var I = 3;

Var s = I. ToString ();

Var s2 = Convert. ToString (I );

Var s3 = (string) I;

Array definition:

Var lis = [1, 3]

Int [] list = {1, 2 };

Array traversal:

For item in list {}

Foreach (var item in list ){}

Dictionary definition:

Var dic = ["a": "v1", "B": "v2"]

Var dic = new Dictionary <string, string> ();

Dic. Add ("a", "v1 ");

Dic. Add ("B", "v2 ");

Dictionary traversal:

For (key, value) in dic

{

Var k = key

Var v = value

}

Foreach (var item in dic)

{

Var k = item. Key;

Var v = item. Value;

}

Range:

For I in 0... 200 {}

For (int I = 0; I <= 200; I ++ ){}

Function Definition:

Func getName (a: Int)-> Bool

Bool GetName (int)

Variable parameters:

Func getName (a: Int ...) -> Bool {}

Bool GetName (params int [] items ){}

Attribute definition:

Var name: String {

Get {}

Set {}

}

String Name {get; set ;}

Modifier:

Public internal private

The meaning is the same as that of C,

The default value is internal.

But no Protect

Public internal private protect

Constructor:

Class MyClass {

Init () // This is the name

}

Class MyClass {

MyClass () // the same name as the class

}

Optional type:

Null type

Var I: Int? = 3

Value: I!

Int? I = 3;

Value: I. Value

(Base) parameters:

Self, super

This, base

Enumeration definition:

Enum ABCF: Int {

Case A = 1, B, C

Case F

}

Enum ABCF {

Case

Case getName (Int)

}

Abnormal ~~~

Enum ABCF {

A,

B = 1,

C

}

Only numeric constants, no characters or methods

Extended attributes:

Extension Method:

Extenstion Int {

Var I

Func toString ()

}

Public static class XXXx

{

Public static string ToString (this int)

{

Return a. ToString ();

}

}

Protocol:

Interface:

Protocol MAction {}

Public interface IMAction {}

 

Summary:

Because Swift is a mix of frontend and backend languages, most of the syntaxes are consistent:

Including operators, branches, loops, and class inheritance.

There are also very similar lamda expressions for anonymous functions.

Swift has a closure, and the JavaScript component is not lightweight.

It's too small to get involved, so I can only talk so much about it ~~

 

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.