How to Declare a String Variable in Swift?

Declare String Variable

To declare a variable of type String in Swift, use : String after the variable name.

var company: String 

Swift Example

Let us write a swift program, in which we declare a variable with the name company, and declare this variable as type String. Later, we will assign a value "Apple" to this variable and print it.

main.swift

import Foundation

var company: String
company = "Apple"
print(company)

Program Output

Apple

The type of the variable company is String, because of which we could assign a String value to the variable.

We can also programmatically check the type of the variable company using type(of:) function.

main.swift

import Foundation

var company: String
print(type(of: company))

Program Output

String

Summary

We have learnt how to declare a variable of type String in Swift.