Swift identifier and keyword, swift identifier and

Source: Internet
Author: User

Swift identifier and keyword, swift identifier and

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 domestic Swift book "Swift development guide" for discussion. Website: http://www.51work6.com/swift.phpwelcome to the swifttechnology discussion group: 362298.pdf

Welcome to Zhijie iOS public classroom Platform



The identifier cannot be a C keyword.

C... it refers to the C language. As for what is the C language available on the internet, identifiers refer to the variables you define in the language, and keywords are reserved for processing, such as int...

What are the identifiers and keywords in c?

An identifier is the name, such as the keyword name, variable name, and so on. It is a separate string.

The keyword is the identifier defined by the compiler, such as int define for if.

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.