Swift-001-swift Introduction and Basic grammar

Source: Internet
Author: User

1.Introduction to Swift

The new development language, released by WWDC in 2014, works with OC on Mac OS and IOS platforms to build apps based on Apple platforms.

After the swift2.0, the syntax was stable, and the swift4.0, released in 2017, changed, but only added some new features. These new features need to be run on Xcode9 to show the effect. It is worth mentioning that it supports UNICODE9, which means that you can use some image icons to act as variables.

2. Features :

① is fast , secure , uses secure programming patterns and adds many new features to make programming simpler and more scalable ( supports cocoa and Cocoa Touch frame ).

② cancels the use of oc pointers and other unsafe accesses . Variables are always initialized before use, arrays and integers are checked for overflow , and memory is automatically managed .

③ supports code previews that allow programmers to run Swift code without compiling and running the application and view the results in real time

④ discards OC's early Smalltalk syntax, which is fully changed to a dot call ( using point syntax to invoke a property or function ).

⑤ provides a Java-like namespace (name space)/ generics (generic)/ operand overloading (operator overloading

the ⑥ syntax is very concise , making the programming process much shorter , simpler, more efficient , and more powerful.

⑦ removing the NS prefix

3. Why to learn Swift:

Swift, as a protocol-oriented language, can not only write the mobile side, but also build the server side . Throughout the iOS development community at home and abroad, many companies have adopted swift development directly or indirectly, and using swift language development has become a trend for future iOS development. Swift's simplicity, elegance and other advantages quickly capture the favor of the vast majority of developers.

Ii. experience swift development with playground

Open Xcode and choose to create a playground project

Left code area, right display area (compile result)

Create a normal UIView object

The left side of the Playgound file is the code area, and the right is the area where the results are displayed. Displays the interface effect in real time when clicked for eyes

You can quickly see (quick look) and display the results immediately (show result) as shown in:

significant differences between Swift and OC :

1> there are no. h and. m files in Swift. All the code is stored in a single file. All the code in Swift is encapsulated in {}

2>oc is initialized with Alloc init, while Swift uses ()

The method is invoked using [] in 3>oc, and the point syntax is used in Swift. Like uicolor.red.

You do not need to split the statement with semicolons in 4>swift

5> does not need #import import header file, it can be used directly,

6>Print:nslog, True and False override Yes and No

The 7> function and the closure use Func to declare a function, using the name and arguments to invoke the function. Use to specify the function return value.

The 8> subclass is defined by adding the name of the parent class after their class name, separated by a colon. Subclass if you want to override the method of the parent class, you need to use the override tag

9> enumerations and structs use an enum to create an enumeration that uses a struct to create a struct. Structs and classes have many of the same places, such as methods and constructors. One of the biggest differences between the structures is that the struct is a value, and the class is a reference

10> interfaces and extensions: Using protocol to declare an interface

Three, constants and variables

1> Data type:

Common data type variables:

But in fact, in Swift, there is no basic data type, so-called data types, in fact, are just the structure of the body

2. Variables and constants:

Statement:

In Swift, declare the constant with let, and declare the variable with var .

A constant cannot modify his value

Error demonstration, let Z no assignment constants are immutable at the time of declaration and can only be assigned at declaration time

In development, it is often preferable to use let, because immutable is a little more secure. It is recommended that when writing code, choose Let, and then change to Var when needed.

Automatic derivation:

Creates a uiview that does not specify a type. You can see that the UIView information is printed on the console. This phenomenon is called automatic derivation of Swift. In fact, the type defined on the left side of the code is just the type that the programmer wants, and the right side is the type of the program .

That is, the type of the variable or constant will derive the corresponding type according to the result of the right-hand code execution.

You can use the Hotkey option to click View Type.

Strict: in swift, no direct operation is allowed between any different types of data .

If you want to allow operations between different types of data, you can convert one of the types

At this point the result is 25. In Swift, the type conversion is done by enclosing the data , which is equivalent to the constructor in the swift struct.

Of course, you can also convert the preceding integers to double type. You can print out decimals at this point.

The result is 25.5.

Specify the type :

Swift-001-swift Introduction and Basic grammar

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.