Swift identifier and keyword

Source: Internet
Author: User

Any computer language is inseparable from identifiers and keywords. The following describes the swift identifiers and keywords in detail.

Identifier

An identifier is the name specified for a variable, constant, method, function, enumeration, struct, class, protocol, etc. There are certain specifications for the letters that make up the identifiers. The naming rules for identifiers in swift are as follows:

Case Sensitive. myname and myname are two different identifiers;

The identifier can start with an underscore (_) or a letter, but cannot start with a number;

Other characters in the identifier can be underscores (_), letters, or numbers.

For example, identifier, username, user_name, _ sys_val, and height are valid identifiers, while 2 Mail, Room #, and class are invalid identifiers. The variables named after the Chinese "height" are valid.

Note that letters in swift are encoded in Unicode [1]. Unicode is called a unified encoding system. It contains Asian characters, such as Chinese, Japanese, and Korean characters, and even the emojis we use in chat tools, such, these symbols are actually Unicode, not images. These symbols can be used in swift.

If you must use keywords as identifiers, you can add the accent symbol (') before and after the keywords, for example:

Let π = 3.14159

 

Let_hello = "hello"

 

Let Hello = "Hello World"

 

Let 'class' = ""

 

// Noah's Ark

Let

 

Here, class is a keyword. In fact, the accent symbol (') is not part of the identifier. It can also be used for other identifiers, such as π and 'π', which are equivalent. Therefore, using keywords as identifiers is a bad programming habit.


Keywords

A keyword is a reserved character sequence similar to an identifier. It cannot be used as an identifier unless it is enclosed by an accent. A keyword is a predefined identifier reserved for the compiler. There are four common keywords.

Declaration-related keywords: Class, deinit, Enum, extension, func, import, init, let, protocol, static, struct, subscript, typealias, and var.

Statement-related keywords: Break, case, continue, default, do, else, fallthrough, if, in, for, return, switch, where, and while.

Expression and type Keyword: as, dynamictype, is, new, super, self, self, type, _ column _, _ file _, _ function _, and _ line __.

Keywords used in specific contexts: associativity, didset, get, infix, inout, left, mutating, none, nonmutating, operator, override, Postfix, precedence, prefix, rightset, unowned, unowned (SAFE), unowned (unsafe), weak, and willset.

For the above keywords, we do not need to know all their meanings at present, but remember: In swift, the keywords are case-sensitive, so the Class and Class are different, of course, class is not a keyword of SWIFT.



For more information, please refer to the First SWIFT book swift development guide in China.

This book discussion site: http://www.51work6.com/swift.php

Welcome to the swift technology discussion group: 362298485

Welcome to Zhijie IOS public classroom Platform



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.