Beginner

I don’t know if this is the right section to post this in. I am brand new to programming and decided to start learning Swift with an app on my iPhone. I understand the basic concept of var and let; I just started learning about Bools.

Can anyone help me to understand when I use : vs. = in Swift? What is the difference between creating and declaring a variable or constant? Is there a site or app where I can practice programming in Swift?

Thank You.

Hi @Goose24 and welcome! The best way to start is to read the Swift Programming Language Guide (the link redirects to the Constants and Variables section, which contains references to how to type annotate variables — you'll find there what's the difference between : and =).

As for practicing, you can use Xcode on macOS or the Swift Playgrounds app on either macOS or iPadOS.

2 Likes

To answer your specific question, the : is used to specify the type of a variable, as in var i : Int. Swift is pretty good at inferring the type of a variable, but, sometimes needs a little help, so you sometimes have to explicitly state the type of the variable, or, sometimes you explicitly type the variable to remind yourself. Another example is in var x = 3, where you really want x to be a Double. The compiler is going to infer that x is actually an Int so that you have to do something like var x : Double = 3 to get what you want.

The = is an operator used to set an initial value, in the context of your question. So, let i : Int = 3 explicitly states that i is unequivocally an Int with an initial value of 3. You could also write let i = 3 and the compiler will do some more work to infer that the type of i is an Int.

But, as @xAlien95 stated about, the Swift Programming Language book goes into a lot more detail about variables, types, properties, initialization, and related topics. It is going to be your new best friend when it comes to Swift.

3 Likes

Hello, and welcome!

Others have already recommended "The Swift Programming Language" — I'm the primary writer currently working on that book. It's always been a goal that TSPL should be accessible to programmers of any background, but it does expect you have some programming experience. If you're new to programming, here's another book from Apple that discusses the basics in more detail, and includes some information about how to work with Swift code in Xcode and in Terminal:

https://books.apple.com/us/book/develop-in-swift-fundamentals/id1511184145

TSPL is also available on Apple Books and as a plain ePUB file, for offline reading. The content in all three places (web, plain ePUB, and Apple Books) is the same.

https://swift.org/documentation/

Hi Goose24,
I'm also a beginning programmer. I'm learning the basics using Paul Hudson's website, Hacking with Swift (100 Days of SwiftUI – Hacking with Swift). Paul provides both text and 1 minute videos you can watch to learn the basics, as well as intermediate programming for free.
Hope this helps.

Terms of Service

Privacy Policy

Cookie Policy