Swift Keywords (original) (raw)

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:

Keywords used in the declaration

Following are the keywords used in the declaration:

associatedtype class deinit enum extension fileprivate func import
init inout internal let open operator private precedencegroup
protocol public rethrows static struct subscript typealias var

Example:

Swift `

// Swift program to illustrate Keywords used in the declaration import Swift

// Creating structure // Using struct keyword struct geeksforgeeks{

// Creating variable 
// using var keyword
var employeeName = "Mohit"
var employeeID = 1234

}

// Creating the instance of geeksforgeeks structure var myinstance = geeksforgeeks()

// Accessing the properties of geeksforgeeks structure print("Employee Name:", myinstance.employeeName) print("Employee ID:", myinstance.employeeID)

`

Output:

Employee Name: Mohit Employee ID: 1234

Keywords used in statements

Following are the keywords used in the statement:

break case catch continue default defer
do else fallthrough for guard if
in repeat return throw switch where while

Example:

Swift `

// Swift program to illustrate Keywords used in statements import Swift

// Finding the age group let EmpAge = 70

// Using if keyword for the condition if (EmpAge >= 60) {

// Print statement
print("Senior Citizen")

}

// Using else keyword for the condition else if (EmpAge >= 40) {

// Print statement
print("Middle age")

}

else {

// Print statement
print("Young")

}

`

Output:

Senior Citizen

Keywords used in expression and type

Following are the keywords used in the expression and type:

Any as catch false is
nil rethrows self Self super
throw throws true try

Example:

Swift `

// Swift program to illustrate Keywords used in expression and type import Swift

// Creating a class class GeeksforGeeks {

// Class method
func EmployeName() 
{ 
    print("Hello! My name is Govind") 
    
}

}

// Creating another class // It is the child class of GeeksforGeeks class Gemployee : GeeksforGeeks {

// Overriding method
override func EmployeName()
{

    // Here we are accessing the parent class method 
    // Using super keyword
    super.EmployeName() 
    print("I m working in the HR department")
}

}

// Creating the object of the child class let obj = Gemployee()

// Accessing the method of child class obj.EmployeName()

`

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:

associativity convenience didSet dynamic final get indirect infix lazy
left mutating none nonmutating optional override postfix precedence prefix
Protocol required right set some Type unowned weak willSet

Example:

Swift `

// Swift program to illustrate Keywords used in the specific context import Swift

// Creating a structure struct GeeksforGeeks {

// Creating variables
var myval: String

// Creating a function that will mutate
// Using mutate keyword
mutating func myfunction() {
    self.myval = "Mohit"
}

}

// Creating variable of GeeksforGeeks structure var obj = GeeksforGeeks(myval: "Rohan")

// Calling the function obj.myfunction() print(obj.myval)

`

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:

Swift `

// Swift program to illustrate Keywords that begin with the number sign import Swift

// Creating a function func GeeksforGeeks() { // Here we use #function keyword to // display the name of the function print("I am the (#function)") }

// Calling the function GeeksforGeeks()

`

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:

Swift `

// Swift program to illustrate keyword used in the patterns(_) import Swift

// Printing GeeksforGeeks 14 time // Using _keyword for _ in 1...10 { print("GeeksforGeeks") }

`

Output:

GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks