TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Question on Constants

Forums > 100 Days of SwiftUI

Hi guys,

I just started out this course and today is my first. I have question regarding constants. I'm aware that constants means the values once set and it cannot be change. But i tried it on playground and changed the value, it still show the value i changed on the right. So which part of it doesnt change?

3      

Hi,

can you show the code you tried?

4      

Hi @VicLee92

A Constant (let) you can not change eg

let myName = "Nigel"

However a Variable (var) you can change

var myAge = 30

and when you change it

myAge = 31

but if you tried

myName = "Paul"

the xcode will complain and say that

Cannot assign to property: 'myName' is a 'let' constant.

Change 'let' to 'var' to make it mutable.

It a good idea that you make properties that you do not want to change let as if you make a mistake later in your code and try to change it then xcode will warn you.

4      

Hi @NigelGee

Now i understand. Thanks for teh guide. I wasnt seeing any error till i tried it again. Thank you again

Hi @nemecek-filip

I guess Nigel just helped me out. But thanks for asking

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.