100% found this document useful (4 votes)
222 views46 pages

Xcode Basics and Swift Programming Concepts

The document is a presentation on Xcode and Swift Programming by Mrs. P.V. Kavitha at SRI Ramakrishna Engineering College. It covers topics such as the iOS environment setup, features of Swift, programming concepts, built-in data types, and various programming constructs including variables, constants, decision-making statements, and data structures like arrays and dictionaries. The presentation aims to provide a comprehensive overview of using Xcode for iOS application development with Swift programming language.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
222 views46 pages

Xcode Basics and Swift Programming Concepts

The document is a presentation on Xcode and Swift Programming by Mrs. P.V. Kavitha at SRI Ramakrishna Engineering College. It covers topics such as the iOS environment setup, features of Swift, programming concepts, built-in data types, and various programming constructs including variables, constants, decision-making statements, and data structures like arrays and dictionaries. The presentation aims to provide a comprehensive overview of using Xcode for iOS application development with Swift programming language.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

SRI RAMAKRISHNA ENGINEERING COLLEGE

[Educational Service : SNR Sons Charitable Trust]


[Autonomous Institution, Reaccredited by NAAC with ‘A+’ Grade]
[Approved by AICTE and Permanently Affiliated to Anna University, Chennai]
[ISO 9001:2015 Certified and all Eligible Programmes Accredited by NBA]
VATTAMALAIPALAYAM, N.G.G.O. COLONY POST, COIMBATORE – 641 022.

Department of Artificial Intelligence and Data Science

Xcode and Swift Programming

P r e s e n ta ti on by
Mrs. P.V. Kavitha
A s s i s tant P r o f e s s or ( S l . Gr)/ A I& D S
Agenda
❑ Xcode, Playground, Simulator
❑ iOS Environmental Setup
❑ Swift
❑ Swift Different Versions
❑ Swift Features and Concepts
❑ Swift Collection Framework

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 2


Android and iOS
Android and iOS ➢ Xcode - IDE
➢ Android Studio -IDE

➢ Java Programming ➢ Swift Programming


➢ Emulator ➢ Simulator

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 3


Xcode and Simulator
▪Xcode is an integrated development ▪ Simulator in Xcode is used to build
environment for macOS containing a and run iOS application
suite of software development tools
▪ It helps us to know how our app will
▪ Developed by Apple for developing look and behave if it is running on
software for macOS, iOS, iPadOS, device
watchOS, and tvOS

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 4


iOS Xcode Environment Setup
❑ Download Xcode editor from the Apple website or Apple app
store and this editor is completely free we don’t need to pay anything for
Apple
❑ The requirement of the Xcode editor is that we should have a minimum
Apple Laptop or Mac Computer for development of the iOS applications
❑ Latest Version - Xcode 13

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 5


Working of Xcode Playground
→ Xcode is an integrated development environment (IDE) for macOS containing a suite
of software development tools developed by Apple for developing software for macOS
→ First released in 2003, the latest stable release is version 11.3 and is available via the Mac
App Store

1 2

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 6


Working of Xcode Playground
→ The Xcode provides all the things
like interfaces for designing
applications, editors for writing the
code, debuggers to debug the code
and different options to build and
run our applications.

→ The main areas in Xcode which we


use most frequently to design, code,
build and run our applications

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 7


Swift
→ Swift is a new programming language developed by Apple Inc for iOS and OS X
development.
→ Swift was begun in July 2010 by Chris Lattner
→ Swift took language ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, etc.
→ Swift provides a Playground platform
→ Xcode software to start Swift coding in Playground
→ Xcode IDE for iOS/OS X application development

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 8


Swift Different Versions Version Released Date

Swift 5.0 March 25, 2019

Version Released Date Swift 5.1 September 10, 2019

Swift 1.0 September 9, 2014 Swift 5.2 March 24, 2020


Swift 1.1 October 22, 2014 Swift 5.3 September 2020
Swift 1.2 April 8, 2015 Swift 5.4 April 2021
Swift 2.0 September 21, 2015
Swift 5.5 June 2021
Swift 2.1 October 20, 2015
Swift 5.6 March 2022
Swift 3.0 September 13, 2016
Swift 5.7 September 2022
Swift 3.1 March 27, 2017
Swift 5.8 March 2023
Swift 4.0 September 19, 2017
Swift 4.1 March 29, 2018 Swift 5.9 2024

Swift 4.2 September 17, 2018 Swift 6.1 2025


01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 9
Features
→ Swift makes use of safe programming patterns
→ Swift provides modern programming features
→ Swift provides Objective-C like syntax
→ Swift is a fantastic way to write iOS and OS X apps
→ Swift unifies the procedural and object-oriented portions of the language
→ Swift does not need a separate library import to support functionalities like input/output or
string handling.

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 10


Create a program for iOS playground -
Swift
import UIKit
var myString = "Hello, Playground!"
print(myString)

Comment Line in Swift


Output: Multi-line comments start with /* and terminate
Hello, Playground with the characters */
Single-line comments are written using // at the
beginning of the comment

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 11


Built-in Data Types
Int or UInt − This is used for whole numbers
Float − This is used to represent a 32-bit floating-point number and numbers with
smaller decimal points
Double − This is used to represent a 64-bit floating-point number and used when
floating-point values must be very large
Bool − This represents a Boolean value which is either true or false
String − This is an ordered collection of characters. For example, "Hello, World!"
Character − This is a single-character string literal. For example, "C"
Tuples - This is used to group multiple values in single Compound Value
Optional − This represents a variable that can hold either a value or no value.

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 12


Declaring variables & constants
• Constants are declared using the let
• Explicitly declaring variable with
keyword, and variables with
datatype
the var keyword
• Use let to make a constant and var to var value:Int
make a variable. The value of a constant value=3
doesn’t need to be known at compile print(value)
time, but you must assign it a value
exactly once • To print the current value of a constant or
variable
import UIKit
var myVariable = 42 var s=“SREC”
let myConstant = 42 print(“Our College Name is \(s)”)

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 13


Variables & Constants

Variables

var weight → Mutable


(They can Change)

Constant

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 14


Displaying a String

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 15


Numbers
import UIKit
var str = "Hello, playground"
print(str)
let sample=21
print(sample)
//Arithmetic operations
var a=10
var b=20
var c=a+b
print(c)

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 16


Numbers

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 17


Printing the values of variable
If you need to convert a value to a different
let apples=5 type, explicitly make an instance of the
let oranges=9 desired type.
let fruits="i have \(apples) apples"
print(fruits) let label = "The width is "
let veg="i have \(apples+oranges) fruits" let width = 94
print(veg) let widthLabel = label + String(width)
Use three double quotation marks (""") for
strings that take up multiple lines.
let quotation = """
I said "I have \(apples) apples."
And then I said "I have \(apples + oranges)
pieces of fruit."
"""

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 18


Decision making statements
if statement: An if statement consists of a Boolean expression followed by one
or more statements
if...else statement: An if statement can be followed by an optional else
statement, which executes when the Boolean expression is false
if...else if...else statement: An if statement can be followed by an optional else
if...else statement, which is very useful to test various conditions using single
if...else if statement
switch case statement: A switch case statement allows a variable to be tested for
equality against a list of values.

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 19


Decision making statements

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 20


Decision making statements

bankBalance = 600

itemToBuy = 700

itemToBuy = 800
bankBalance = 800

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 21


Decision making statements
Ternary Operator (?:) Switch Case Statement

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 22


Looping Statements
for-in : This loop performs a set of statements for each item in a range, sequence,
collection, or progression.
while loop: Repeats a statement or group of statements while a given condition
is true. It tests the condition before executing the loop body.
repeat...while loop: Like a while statement, except that it tests the condition at
the end of the loop body.

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 23


For loop

Repeat … While loop

While loop

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 24


for loop

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 25


Swift – Transfer Statements
➢ continue statement is used to stop executing the next statements in loop
and start from the beginning of the next iteration of loop
➢ break statement will stop or ends execution of current flow statements
immediately
➢ fallthrough statement is used in switch statement to execute case
statements which are next to the matched case statements
➢ return statement is used in functions or methods to return the values

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 26


Swift – Transfer Statements
continue break

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 27


Transfer Statements
fallthrough return

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 28


Swift - Tuples
→ Tuples type, are used to group multiple values in a single compound Value.
→ The values in a tuple can be of any type, and do not need to be of same type.
→ You can create tuples from as many values as you want and from any number of different data
types.

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 29


Swift - Sets
→ Sets are used to store distinct values of same types but they don’t have definite ordering as
arrays have.
→ Use sets instead of arrays if ordering of elements is not an issue, and there are no duplicate
values. (sets allow only distinct values.)
→ All the basic swift values are of hashable type by default and may be used as set values.

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 30


Accessing and modifying Sets
Access or modify a set using its methods and properties −
"count" method can be used to show the number of elements in the set.
var someSet=[10,30,20]
print(someSet.count) ➔ 3
“append" method can be used to add values in set.
someSet.append(40) // adds the element to Set.
print(someSet.count) ➔ 4
Similarly, isEmpty can be used to check if set is empty.
someSet.isEmpty // returns true or false depending on the set Elements.
print(someSet.isEmpty) ➔ false
someSet.contains(30) // to check if set contains this value.
print(someSet.contains(30)) ➔ true

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 31


Performing Set Operations
Perform basic set operations on swift Sets.
Following are the methods for performing set operations:
→ Intersection
→ Union
→ Subtracting

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 32


Arrays
→ Arrays are used to store ordered lists of values of the same type
→ If you assign a created array to a variable, then it is always mutable, which means you can
change it by adding, removing, or changing its items; but if you assign an array to a constant,
then that array is immutable, and its size and contents cannot be changed
→ Create an array of three elements and assign three values to the array
→ var a:[Int]=[10,20,30]

→ Accessing arrays
→ print(a)
→ print(a[0])

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 33


Arrays

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 34


Arrays

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 35


Adding Two Arrays Modifying Arrays
Use the addition operator (+) to add two
arrays of the same type which will yield a
new array with a combination of values
from the two arrays as follows:
var intsA = [Int](repeating: 2,count:2)
var intsB = [Int](repeating: 1,count:3)
var intsC = intsA + intsB
for item in intsC { Output:
2
print(item)
2
} 1
1
1

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 36


Enumerated
use enumerated() function which returns the index of an item along with its value

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 37


count property empty property

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 38


Characters
A character in Swift is a single character String literal, addressed by the data
type Character.

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 39


String Operations

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 40


Dictionary
→ Dictionaries are used to store unordered lists of values of the same type
→ Dictionaries use unique identifier known as a key to store a value which later can be
referenced and looked up through the same key
→ A dictionary key can be either an integer or a string without a restriction, but it should be
unique within a dictionary.
→ If assigned a created dictionary to a variable, then it is always mutable which means it can
change by adding, removing, or changing its items. But if assigned a dictionary to a constant,
then that dictionary is immutable, and its size and contents cannot be changed

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 41


Creating and Accessing Dictionary
Create an empty dictionary whose key will be of Int type and the associated values will
be strings
var someDict = [Int: String]()
Here is an example to create a dictionary from a set of given values:
var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
print(someDict)

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 42


Sequence Based Initialization
Swift allows to create Dictionary from arrays (Key-Value Pairs.)
var cities = [“Delhi”,”Bangalore”,”Hyderabad”]
Use the following simple syntax to create an empty dictionary whose key will be of Int
type and the associated values will be strings −
var Distance = [2000,10, 620]
Here is an example to create a dictionary from a set of given values −
let cityDistanceDict = Dictionary(uniqueKeysWithValues: zip(cities, Distance))

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 43


Filtering
Swift allows to filter values from a dictionary.
var closeCities = cityDistanceDict.filter { $0.value < 1000 }
If we run the above code closeCities Dictionary will be.
["Bangalore" : 10 , "Hyderabad" : 620]

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 44


Functions – To perform an action/operation
func mult(a1:Int,b1:Int) -> Int{ //Functions with
parameter
return a1*b1 func sum(a:Int,b:Int){//Functions without
} return value
print(mult(a1:2,b1:4)) ➔ 8
let a = a+b
func voters()->String{ //Functions without parameter let b = a-b
return ”Kavitha" print(a,b)}
} sum(a:20,b:10) ➔ 30 20
print(voters()) ➔ Kavitha

func disp(no:Int)->Int{ //Functions with return value


let a=no
return a
}
print(disp(no:200)) ➔ 200

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 45


Thank You

01-07-2025 SWIFT PROGRAMMING - P.V. KAVITHA 46

You might also like