BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Question: why name is a constant in my simple codes ?

Forums > 100 Days of SwiftUI


Hello this is a pretty simple example code in the Playground , but at the line of name = name.uppercased(), Xcode warns me that error: cannot assign to value: 'name' is a 'let' constant

How come name is a constant ? I didn't declare it as a constant or a variable.

import Cocoa

func isUppercase(_ name: String) -> String {
   name = name.uppercased()

let result = isUppercase("Mary")

print (result)


Parameters are passed into functions as constant (i.e., let) values.

Side note: It's a bad idea to name that function isUppercase. Names like is______ in Swift (and most languages) would be expected to return a boolean indicating whether or not the parameter is uppercased. If you want to convert a String to uppercase, name it something else, like uppercase or toUppercase.



@roosterboy thank you as alway !

noted about the convention too.

Happy Lunar New Year and have a great year of tiger !



As @roosterboy say parameters passing in are none mutating however if you want them to be you need to add inout eg

func toUppercase(_ name: inout String) -> String {

And when you call it you need a & in front of the variable that you want to mutate.

var mary = "Mary"
let result = toUppercase(&mary)
print(result) // MARY

More detail explanation What are inout parameters?



@NigelGee thanks for the extensive explanation !

Will look into it !

have a great day,



Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn 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.