Table of contents
1.
Introduction
2.
What is swift
3.
Swift Syntaxes
3.1.
Comments in Swift
3.2.
Semicolons in Swift
3.3.
Whitespaces in Swift
3.4.
Print Statement in Swift
3.5.
Import in Swift
3.6.
Tokens in Swift
3.7.
Identifiers in swift
3.8.
Keywords in swift
3.9.
Literals in swift
4.
Swift program
5.
Frequently Asked Questions
5.1.
What are the important features of swift?
5.2.
Is Swift an object-oriented programming language?
5.3.
What are the control transfer statements of iOS swift?
5.4.
Is Swift a backend or frontend language?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Swift Syntax and Program

Author SAURABH ANAND
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Swift is fast, safe, modern, and enables a level of interactivity in development. Programmers need to understand the fundamentals of Swift syntax and program, including its environment setup, installation procedure, and coding style. Swift provides an appropriate platform for executing Swift syntax and programs. Now we will configure Swift syntax and program for learning purposes. 

In this blog, we will see the basic syntax of the Swift programming language.

What is swift

Swift is a multi-paradigm, compiled programming language for designing apps for iOS, OS X, tvOS, and watchOS. Apple Inc. is the company behind it. It is a powerful and intuitive language that is simple to pick up. Swift code is secure, precise, and speedy.

Swift is based on the Objective-C runtime library, allowing C, Objective-C, C++, and Swift code to run in the same application. Swift is an open-source programming language included in Xcode since version 6.

Swift logo

Source: https://www.stickpng.com/img/icons-logos-emojis/tech-companies/swift-logo

 


Swift 4 is the most recent version of Swift, which includes all of the capabilities of Swift 3 while additionally improving the language's robustness and stability. Swift 4 has updated the standard library, added new capabilities like smart key paths and serialization, and reduced build times and app size.

Check out the Swift Environment setup blog to learn more about the steps for setting up Swift.

Swift Syntaxes

Let us see some basic syntax of Swift syntax and program.

Comments in Swift

Comments are used in software to make it easier to understand. They're like help texts in programs that the compiler ignores. Single-line comments in Swift 4 are typed with // at the start.

Single line comment in Swift 4:

// This is a single-line comment.  

 

Multi-line comment is Swift 4:

/* This is a multiline comment
    CodingNinjas rocks!!
 */ 

Semicolons in Swift

We no longer need to enter a semicolon (;) as a closing statement in Swift 4. Although it is optional, we can use it without difficulty. If we have many statements on the same line, we must use a semicolon as a delimiter, else the compiler will complain about syntax errors.

For example: 

  • With semicolon:
    var myString = "Hello, codingNinjas!"; print(myString)  
  • Without using semicolons:
    var myString = "Hello, codingNinjas!"  
    print(myString) 

Whitespaces in Swift

Blanks, tabs, newline characters, and comments are all described as whitespace in Swift 4. It distinguishes between two parts of the statement. It instructs the computer to recognize where one element terminates and another begins.

For example

  • var codingNinjas  
    To make the compiler distinguish between var and age, we must use at least one whitespace character (typically a space).
  • On the other hand:
    int courses = coding + Ninjas   
    There is no need for blank characters between courses and =, or between = and "coding", though we can include them for greater readability.

Print Statement in Swift

To print anything in Swift4, use the 'print' keyword. The print keyword has three distinct features.

  1. Items: Items that we want to print.
  2. Separator: Used to separate items.
  3. Terminator: It specifies the last value where the line ends.
     

For example

  • print("cout statement", separator: "Value " , terminator: "Value") // E.g. of print statement.  
  • print("Value one") // prints "Value one \n" Adds, \n as terminator and " " as separator by default.  
  • print("Value one","Value two", separator: " Next Value" , terminator: " End") //prints "Value one Next Value Value two End"  

Import in Swift

Any Objective-C framework (or C library) can be imported directly into our Swift program using the import statement. The import cocoa statement makes all Cocoa libraries, APIs, and runtimes that makeup OS X's development layer available in Swift.

Because Cocoa is written in Objective-C, a superset of C, it's simple to include and even C++ into our Swift applications.

Tokens in Swift

A token is a keyword, an identifier, a constant, a string literal, or a symbol in a Swift program.

For example, the following Swift statement contains three tokens

print("text!")

The individual tokens are:

print("text!") i.e, print, (, text.

Identifiers in swift

A Swift identifier identifies a variable, function, or any other user-defined item. An identifier begins with an alphabet (A to Z) or an underscore (_), followed by zero or more letters, underscores, or numerals (0 to 9).

Special characters like @, $, and percent are not allowed in identifiers in Swift 4. Swift is a case-sensitive computer language. Thus, in Swift, Codingninjas and codingninjas are two distinct identifiers.

Some valid Identifiers are: Saurabhanandthor_rangnaroka_12343coding50_tempa23db93retCal

Keywords in swift

The reserved words in a programming language are called keywords. Some of the reserved keywords in Swift are given below.

  • Keywords used in declarations: Class, struct, subscript, var, etc.
  • Keywords used in statements: break, do, if, where, etc.
  • Keywords used in expressions and types: _COLUMN_, _FUNCTION_,  dynamicType, etc.
  • Keywords used in particular contexts: willSet, set, lazy, type, etc.

Literals in swift

A literal is the representation of an integer, floating-point number, or textual value in the source code. Here are some examples of literal.

Example: 

92               // Integer literal

4.26759          // Floating-point literal

"Hello, Ninjas!"  // String literal

Swift program

Let’s look at our first “Hello,world” program in swift.

import UIKit

var str = "Hello, World."

If we develop the identical program for OS X, it will have Cocoa import, and the program will look like this:

import Cocoa

var str = "Hello,World"

Xcode code window

Source: tutorialspoint.com/swift/swift_environment.htm

 

The Playground result area should show the following result (Right Hand Side).

Hello, world

Frequently Asked Questions

What are the important features of swift?

Some important features of Swift are Less code, Enforced initializers, Protocol oriented, etc.

Is Swift an object-oriented programming language?

Yes, Swift is an object-oriented programming language.

What are the control transfer statements of iOS swift?

The control transfer statements that are used in iOS Swift include Return, Break, Continue, and Fallthrough.

Is Swift a backend or frontend language?

Swift can be used to create client-side (frontend) and server-side (backend) software (backend).

Conclusion

In this article, we have extensively discussed the concept of Swift syntax and program. We started with Swift syntax and program introduction and concluded with swift syntax and program.

After reading about the attributes of a file, are you not feeling excited to read/explore more articles on the topic of file systems? Don't worry; Coding Ninjas has you covered. To learn, see the Definitive Swift Tutorial for Beginnerswhy iOS & OS X Developers are choosing Swift?, and which is better to learn? iOS or Android?

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass