VOOZH about

URL: https://www.geeksforgeeks.org/swift/swift-keywords/

⇱ Swift - Keywords - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Swift - Keywords

Last Updated : 5 Dec, 2022

Every programming language has some set of reserved words that are used for some internal process or represent some predefined actions such words are known as keywords or reserve words. You are not allowed to use these reserved words as a variable name, constant name, or any other identifier. But if you want to want to use keywords as identifiers then you have to use the backtick (`) before and after the specified keyword. For example, a struct is a keyword it is not an identifier but `struct` is a valid identifier. backtick does not work with identifiers like `a` and a both are the same. In Swift, keywords are categorized in four categories according to their usage:

  • Keyword in declaration
  • Keyword in statement
  • Keywords in expression and type
  • Keywords in the specific context
  • keywords that begin with the number sign
  • Keyword used in the patterns(_)

Keywords used in the declaration

Following are the keywords used in the declaration:

associatedtypeclassdeinitenumextensionfileprivatefuncimport
initinoutinternalletopenoperatorprivateprecedencegroup
protocolpublicrethrowsstaticstructsubscripttypealiasvar

Example:

Output:

Employee Name: Mohit
Employee ID: 1234

Keywords used in statements

Following are the keywords used in the statement:

breakcasecatchcontinuedefaultdefer
doelsefallthroughforguardif
inrepeatreturnthrowswitchwherewhile

Example:

Output:

Senior Citizen

Keywords used in expression and type

Following are the keywords used in the expression and type:

Anyascatchfalseis
nilrethrowsselfSelfsuper
throwthrowstruetry

Example:

Output:

Hello! My name is Govind
I m working in the HR department

Keywords used in the specific context

In Swift language, some keywords are used in a specific context, and outside the context of the grammar, they are allowed to use as identifiers. Following are the keywords used in the specific context:

associativityconveniencedidSetdynamicfinalgetindirectinfixlazy
leftmutatingnonenonmutatingoptionaloverridepostfixprecedenceprefix
ProtocolrequiredrightsetsomeTypeunownedweakwillSet

Example:

Output:

Mohit

Keywords that begin with the number sign

Following are the keywords that begin with the number sign(#):

#available#colorLiteral#column#dsohandle#elseif#else#endif
#error#fileID#fileLiteral#filePath#file#function#if
#imageLiteral#keyPath#line#selector#sourceLocation#warning

Example:

Output:

I am the GeeksforGeeks()

Keyword used in the patterns(_)

In Swift, underscore(_) keyword is used in patterns. Suppose you want to print a pattern 14 times then you can use this keyword.

Example:

Output:

GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
Comment
Article Tags:
Article Tags:

Explore