Swift Variables and Constants
5 (1)

Click to rate this post!
[Total: 1 Average: 5]

Last Updated on March 13, 2022 by Deya Eldeen

Any meaningful program should contain stores to save data, swift distinguishes between constant and variable pieces of data with let and var keywords, for example, say you have a customer object, their birthdate is a constant, but their balance is a variable, if you declare data as a constant, the compiler will not allow you to have it changed later, even at compile time, this is not only for safety, but constants are generally faster to work with by the processor.

class Customer {
	let id = 22034
	let birthdate = "11/11/2011"
	var balance = 109.3

Constants cannot change after you run your app, they prevent accidental breakage of a value that should not change.

Leave a Reply

Your email address will not be published. Required fields are marked *