Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Iterating Over an Array

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs += ["Google"]
for item in someStrs {
   print(item)
}

String Comparison

// Using == for compare 2 string
var varA = "Hello, Swift 4!"
var varB = "Hello, World!"

if varA == varB {
   print( "\(varA) and \(varB) are equal" )
} else {
   print( "\(varA) and \(varB) are not equal" )
}

String Interpolation

var varA = 20
let constA = 100
var varC:Float = 20.0

var stringA = "\(varA) times \(constA) is equal to \(varC * 100)"
print( stringA )

Empty String

// Empty string creation using String literal
var stringA = ""

if stringA.isEmpty {
   print( "stringA is empty" )
} else {
   print( "stringA is not empty" )
}

// Empty string creation using String instance
let stringB = String()

if stringB.isEmpty {
   print( "stringB is empty" )
} else {
   print( "stringB is not empty" )
}

Optional Binding

var myString:String? =nil
myString = "Hello, Swift 4!"

if let yourString = myString {
   print("Your string has - \(yourString)")
} else {
   print("Your string does not have a value")
}

typealiases

typealias Feet = Int
// default Int is Int64
var distance: Feet = -9223372036854775808
print(distance)

fffdfd

class Circle {
   var radius = 12.5
   var area: String {
     return "of rectangle for \(radius) "
   }
}

class Rectangle: Circle {
   var print = 7
   override var area: String {
      return super.area + " is now overridden as \(print)"
   }
}

let rect = Rectangle()
rect.radius = 25.0
rect.print = 3
print("Radius \(rect.area)")

class Square: Rectangle {
   override var radius: Double {
      didSet {
         print = Int(radius/5.0)+1
      }
   }
}

let sq = Square()
sq.radius = 100.0
print("Radius \(sq.area)")

sadijutt

let _const = "Hello, sadi jutt!"
print(_const)

let 你好 = "你好世界"
print(你好)

Compile and Execute Swift Online

import Foundation
import Glibc
 
let player = ["rock", "paper", "scissors", "lizard", "spock"]
 
srandom(UInt32(NSDate().timeIntervalSince1970))
for count in 1...3 {
    print(count)
}
 
print(player[random() % player.count]);

Compile and Execute Swift Online


   func merge(left:[Int],right:[Int]) -> [Int] {
               var mergedList = [Int]()

            var left = left
            var right = right
            
            while left.count > 0 && right.count > 0 {
                if left.first! < right.first! {
                    mergedList.append(left.removeFirst())
                } else {
                    mergedList.append(right.removeFirst())
                }
                
                               print(mergedList)

            }
            
            return mergedList + left + right
        }
        
        func mergeSort(list:[Int]) -> [Int] {
            guard list.count > 1 else {
                return list
            }
            
            let leftList = Array(list[0..<list.count/2])
            let rightList = Array(list[list.count/2..<list.count])
            
           // print(merge(left: mergeSort(list:leftList), right: mergeSort(list:rightList)))
            
            return merge(left: mergeSort(list:leftList), right: mergeSort(list:rightList))
        } 
        
        
         let arrayss = [98,1,45,13,6]
        

        let sortedArr = mergeSort(list: arrayss)
        
        //print(sortedArr)




        
        
       

Previous 1 ... 6 7 8 9 10 11 12 ... 61 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.