Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views5 pages

Swift

swift

Uploaded by

sanket1shah24
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
0% found this document useful (0 votes)
11 views5 pages

Swift

swift

Uploaded by

sanket1shah24
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/ 5

import UIKit

// Let and Var


//Let - Unmutable
// Var - Mutable
let str = "Sanket" //We can't Change
var str1 = "Abhay"
str1 = "Abhay Singh"
print(str1)

//Print Variable in Swift


var a1 = 10
print("The value of a1 : \(a1)")

//Empty String

let estr = ""


if estr.isEmpty
{
print("String is Empty")
}
else
{
print(estr)
}

//Compare String
var v1="Sanket"
var v2="sanket"
if v1==v2
{
print("Equal")
}
else
{
print("Not Equal")
}

//Type Assignment of Variable

var a2:Int = 12
print(a2)

//Optional and Force Unwrapping


//Optional : If you want to declare any value as nil delare var variablename : datatype ?

var a3:Int?
var a4:String?

print(a3)
print(a4)

//Check the String is Nil or not


if a3 != nil
{
print("String is not nil")
}
else
{
print("String is nil")
}

a3 = 10
a4 = "Sanket"

//Force Unwrapping

print(a3!)
print(a4!)

//Array

var array1 = [Int]() //Empty Array


print(array1)
//How Add Element in Array
array1 = [10,20,30,40,50]
print(array1)

//Other way to Declare an array


let array2:[String] = ["Sanket","Darshan","Darshit"]
print(array2)

let array3 = ["AA","BB","CC","DD"]


print(array3)

//How to Access each and every elements in array


print(array2[0])
print(array2[1])
let arr = """
Hello student....
We will celebrate 2019
"""
print(arr)

//Working with String

let s1 = "Sanket"
let s2 = 26
let s3 = s1 + " is " + String(s2) //Type Casting with String Concatenation
print(s3)

//Dictonary
let ages = ["sanket":26,"Darshit":27]
print(ages["sanket"]!)

//How to store element value, Dictonary to varible


if let temp = ages["Darshit"]
{
print("Darshit is \(temp) year old")
}

//Set
var colors:Set<String> = ["Red","Blue","Green","Green"]
print(colors)

colors.insert("Maroon")
colors.remove("Red")

colors.contains("Blue")
print(colors)
//Tuples
//Tuples are not technically a collection but instead simply multiple varible that can be passed
around with single variable
let fullname = ("Sanket","Shah")
print(fullname.0)
print(fullname.1)

//Tuples are also deconstructed as new varible name

let (first,last) = ("Sanket","Shah")


print(first)
print(last)

let (first1,_) = ("Sanket","Shah")


print(first1)

/*
//Control Flow

if 10 < 5
{
print("10 is less than 5")
}
else
{
print("10 is Greater than 5")

let j1 = 10

switch j1 {
case 9:
print("9 number")
case 10:
print("10 number")
default:
print("Default")
}

let names = ["AAA","BBB","CCC","DDD","EEE","FFF"]

for name in names


{
print(name)
}
var sum = 0
for i in 1...10
{
sum = sum + i
}
print(sum)
for i in 1...10
{
if i % 3 == 0
{
print(i)
}
}

//indices
let z1 = ["aaa","bbb","ccc","ddd","eeee","gggg"]

for a in z1.indices
{
if a < 3
{
print(z1[a])
}
}
for (index,name) in z1.enumerated()
{
print("\(index) : \(name)")
}
*/

You might also like