Li Hongqiang iOS developing Swift -02_ variables and constants

Source: Internet
Author: User

Li Hongqiang iOS developing Swift -02_ variables and constants

First, the performance of the language

(1) According to WWDC's display

OBJECTIVE-C has 2.8 times times the performance of Python when it comes to sorting complex objects, and Swift has 3.9 times times the performance of Python

The performance of objective-c is 127 times times that of Python when implementing the RC4 encryption algorithm, and Swift's performance is 220 times times that of Python.

Sceptical foreign programmers have also tested the performance of objective-c and Swift http://www.splasmata.com/?p=2798

(2) Description

Current performance is not the most worrying issue for developers

Apple has mastered all aspects of the language: from the bottom-up framework to the compiler to the language design

The path to optimization has only just begun, and as a developer, it takes a little patience and confidence to give this new language

Ii. comparison of Swift and OBJECTIVE-C code formats

To add a button as an example

(1) Objective-c language

UIButton *btn = [[UIButton alloc] init];

Btn.frame = CGRectMake (20, 20, 100, 100);

Btn.backgroundcolor = [Uicolor Bluecolor];

[Self.view ADDSUBVIEW:BTN];

(2) Swift language

var btn = UIButton ()

Btn.frame = CGRectMake (20, 20, 100, 100)

Btn.backgroundcolor = Uicolor.bluecolor ()

Self.view.addSubview (BTN)

Three, grammar notice

The source file extension for 1.Swift is. Swift

2 of 2.Swift languages not required

(1) You do not need to write the main function

Executes sequentially from top to bottom, so the front code is automatically used as the entry for the program.

(2) You do not need to add a semicolon after each statement

Let radius = 10 If you like, you can also add allow radius = 10;

Note: There is a case where a semicolon must be added: let radius = 10 when there are multiple statements on the same line of code; Let Radius2 = 15

(3) About annotations

1) Single-line comment

This is a single-line comment

2) Multi-line Comment

/* This is a multiline comment

This is a multiline comment */

3) Multiple lines of comments can be nested in multiple lines of comments

/* Outer Note

/* Inner Layer Comment

Inner Comment */

Outer NOTE */

Iv. variables and Constants

1. Declaring constants and variables

Declare a constant let radius = 10

VAR is used to declare the variable var age = 20 or var x = 0.0, y = 0.0, z = 0.0

2. You can basically use any character you like as a constant and variable name

(1) letπ= 3.14159

(2) Let URL = "http://ios.itcast.cn"

(3) Let???? = "Puppy and cow"

// ?? And?? It's a emoji expression, a special Unicode character.

You can use shortcut keys on your Mac: Control + COMMAND + space, pop-up emoji keyboard

3. Note Points for constants and variable names

Cannot contain mathematical symbols (e.g. + 、-、 *, \)

Cannot contain arrows (e.g. ↑, ↓, ←, →)

Cannot contain illegal invalid Unicode characters (for example?). )

cannot be a keyword (such as Let, Var)

Cannot start with a number (e.g. 123go)

Cannot be a single underscore _ (e.g. var _ = 10)

... ...

Five, the Code demonstration

1. Command line items

Create a new command-line item (note the XCODE6 version when used)

Select the language as the swift language.

  

The simplest program: print helloword!

  

2.ios Project

(1) Description of the project structure

Create a new iOS project

The new project structure is as follows:

  

The storyboard is a square, larger than the previous one, which is first adjusted to 3.5 inches.

Compare the differences with the OC language iOS project:
(1) No prefix

(2). The. h and. m files are combined into one suffix fame. Swift files

The classes in the controller are structured as follows:

  

(2) Add a button and ImageView code example

  

Implementation results:

  

(3) Create a new playground walkthrough program

  

The immediate compilation effect of the walkthrough program:

    

Li Hongqiang iOS developing Swift -02_ variables and constants

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.