UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.