Introduction to Swift

Basic Syntax in Swift

mobile operating system by Apple Inc.

Mobile operating system by Apple Inc.

Swift is a powerful and intuitive programming language developed by Apple for iOS, macOS, watchOS, and tvOS app development. It's designed to give developers more freedom than ever. Swift is easy to use and open source, so anyone with an idea can create something incredible.

Understanding Swift Syntax

Swift syntax is designed to be easy to read and write. It eliminates entire classes of unsafe code. Syntax is the set of rules that dictate how programs written in a language must be structured. Swift's syntax is clear and concise, making it easier for developers to write and understand code.

Writing Your First Swift Program: "Hello, World!"

In Swift, you can write a simple "Hello, World!" program like this:

print("Hello, World!")

When this code is run, it will output the string "Hello, World!" to the console.

Comments in Swift

Comments are used to include non-executable text in your code, as a note or reminder to yourself. Comments are ignored by the Swift compiler when your code is compiled. In Swift, you can create a single line comment using two slashes // and a multiline comment using /* to start the comment and */ to end it.

// This is a single line comment /* This is a multi-line comment */

Swift Identifiers, Keywords and Literals

Identifiers are names used to identify a variable, function, or any other user-defined item. In Swift, an identifier can start with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9).

Swift reserves certain names as keywords, which have special meaning in the context of the language. For example, if, else, while, do, try, etc. are keywords in Swift.

A literal is a direct value embedded in your source code, like the number 5 or the string "Hello". Swift uses literals to initialize variables or constants for numbers, strings, arrays, dictionaries, and booleans.

var myVariable = 10 // 'myVariable' is an identifier, '10' is a literal let myConstant = "Hello, World!" // 'myConstant' is an identifier, '"Hello, World!"' is a literal

Understanding the basic syntax of Swift is the first step towards mastering this powerful language. As you continue to learn, you'll discover that Swift's syntax is not only easy to understand, but also a joy to use in your everyday coding.