Swift Variables and Constants
5 (1)

Last Updated on November 14, 2021 by deya

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.

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

Leave a Reply

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