Swift Coding

Article by: Manish Methani

Last Updated: October 14, 2021 at 2:04pm IST
3 min 8 sec read

In this swift tutorial we will create our first Swift Program with Playground in Xcode,

Open Xcode , File -> New -> Playground > Enter Playground Name > Choose platform iOS > Next. You will see a playground screen like this,

Swift First Program

import UIKit

var str = "Hello, playground"
print(str)

Output :-

You see output at bottom of the playground. Quite easy and handy tool. Right ?

 

import statement is used to import Objective-C frameworks which includes libraries etc.

print is used to print the variable named str.

; semicolon is not necessary in Swift language unless you don't write multiple statements on a single line.

How do you write comments in Swift Programming?

Comments are used to ease the program understanding. What will this particular function will do is written in Comments so that when you or any other programmer refers the code, it will be easy to understand.

There are two ways you can write the comments in Swift Programming

  • Single line comment
  • Multi-line comment

Identifier

The Identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z, a to z, or an underscore '_' followed by zero or more letters, underscores, and digits (0 to 9). Swift does not allow punctuation characters such as @, $, and % within identifiers. Swift is a case-sensitive programming language. Thus, Programming and programming are two different identifiers in Swift.

abc , _abc , abc_123 are valid identifiers
whereas,
$abc,#abc,abc$ are not valid identifiers . 

Tokens in Swift

A Swift program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. There are 4 tokens in the given example below.

print                           --> 1st token
(                                --> 2nd token
str                              --> 3rd token
)                                --> 4th token            

Semicolon in Swift

Unlike many other languages, Swift does not require you to write a semicolon (;) after each statement in your code, although you can do so if you wish. However, semicolons are required if you want to write multiple separate statements on a single line.

let name = "Manish"; print(name)

Explore Codzify Courses

PREMIUM

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Course Fees: INR 6999/-

Apply Coupon Code: UNLOCK

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

Flutter Mobile App Development Course

Level: Intermediate

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

The Complete Angular Course

Level: Beginners

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

Dart Programming for Absolute Beginners

Level: Beginners

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Coming Soon
FREE

Next.js course

Level: Beginners

Course Fees: FREE

Start Watching
FREE

Fundamentals of Computer Programming Languages

Level: Beginners

Course Fees: FREE

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Level: Beginners

Course Fees: FREE

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer