Swift – Keywords
Last Updated :
05 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:
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
import Swift
struct geeksforgeeks{
var employeeName = "Mohit"
var employeeID = 1234
}
var myinstance = geeksforgeeks()
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
import Swift
let EmpAge = 70
if (EmpAge >= 60)
{
print ( "Senior Citizen" )
}
else if (EmpAge >= 40)
{
print ( "Middle age" )
}
else
{
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
import Swift
class GeeksforGeeks
{
func EmployeName()
{
print ( "Hello! My name is Govind" )
}
}
class Gemployee : GeeksforGeeks
{
override func EmployeName()
{
super .EmployeName()
print ( "I m working in the HR department" )
}
}
let obj = Gemployee()
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
import Swift
struct GeeksforGeeks
{
var myval: String
mutating func myfunction() {
self .myval = "Mohit"
}
}
var obj = GeeksforGeeks(myval: "Rohan" )
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
import Swift
func GeeksforGeeks()
{
print ( "I am 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
import Swift
for _ in 1...10 {
print ( "GeeksforGeeks" )
}
|
Output:
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
Share your thoughts in the comments
Please Login to comment...