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

NSRegularExpression_RangeOfFirstMatch

import Foundation

do {
    let inputString = "TestText"
    let nsInputString = NSString(string: inputString)
    
    let regex = try NSRegularExpression(pattern: "Te[sx]t\\s?", options: [.caseInsensitive])
    let searchRange = NSRange(location: 0, length: nsInputString.length)

    let firstMatchRange = regex.rangeOfFirstMatch(in: inputString, options: [], range: searchRange)

    if !NSEqualRanges(firstMatchRange, NSRange(location: NSNotFound, length: 0)) {
        let firstMatchString = nsInputString.substring(with: firstMatchRange)
        print("Match found: \(firstMatchString)")
    } else {
        print("No match found!")
    }
} catch {
    print(error)
}

21324

protocol stname {
   var name: String { get }
}
protocol stage {
   var age: Int { get }
}
struct Person: stname, stage {
   var name: String
   var age: Int
}
func print(student: stname & stage) {
   print("\(student.name) is \'(student.age) years old")
}
let studname = (name: "Shehan", age: 21)
print(studname)

let stud = Person(name: "Rehan", age: 29)
print(stud)

let student = Person(name: "Roshan", age: 19)
print(student)

Counting Palindromes

import Foundation
import Glibc
 
let sentance = "madam anna kayak notapalindrome anna Civic racecar kayak"

// output shouould be like this -:
// ["Civic":1, "madam":1, "anna":2, "kayak":1,"racecar":1]

func allPalinCounts(Sentance: String) -> [String: Int]
{
    var counts = [String: Int]()
    let words = sentance.components(separatedBy: " ")
   // print(words)
    
    for wordd in words
    {
        // print(word)
         if isPalin(word : wordd)
         {
         let count = counts[wordd] ?? 0
         print(count)
         counts[wordd] = count + 1 
             // print("Found palind")
         }
         
    }
    
    return counts
}

func isPalin(word : String) -> Bool
{
var currentIndex = 0
let charactres = Array(word.lowercased())
while currentIndex < word.count/2
{
if charactres[currentIndex] !=  charactres[charactres.count - currentIndex - 1]
{
    return false
}
    currentIndex += 1
}
    
    return true
}


let vaue = allPalinCounts(Sentance: sentance)
print(vaue)

Enumerations

import Foundation
import Glibc

enum Foods {
    case apple, banana, cake, hotdog, rice, potato
}

enum MoreFoods {
    case apple 
    case banana
    case cake
    case rice(kcal: Int)
    case hotdog
}

func showFoodsNames ( food: MoreFoods ) -> String? {

    switch food {
        case .cake, .hotdog :
            return nil
        /*case .rice, .banana :
            return "It's acceptable"*/
        case .rice( let kcal ) where kcal < 100 : 
            return "good calorie number for this food"
        default:
            return "Good for health"
    }
    
}

let goodFood = showFoodsNames( food : MoreFoods.rice( kcal: 80 ) ) ?? "not a good food for cutting"

print(goodFood)


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]);

Closures Program

let studname = { print("Welcome to Swift Closures") }
studname()

Calling a Function

func display(no1: Int) -> Int {
   let a = no1
   return a
}

print(display(no1: 100))
print(display(no1: 200))

func sum1(no1:Int, no2:Int) -> Int {
    return no1 + no2
}
print("Sum of 1 and 2: \(sum1(no1: 1, no2: 2))")

func sum2(_ no1:Int, _ no2:Int) -> Int {
    return no1 + no2
}
print(sum2(1,2))

func displaySum(_ no1: Int, _ no2: Int) {
    print(no1 + no2)
}
// print(displaySum(4,5)) // => should be use: displaySum(4,5)

func noReturn() {
    var a = 1
    a += 2
}
noReturn()

Empty Property

var someDict1:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var someDict2:[Int:String] = [4:"Four", 5:"Five"]
var someDict3:[Int:String] = [Int:String]()

// count property
print("Total element in someDict1: \(someDict1.count)")
print("someDict1 = \(someDict1.isEmpty)")
print("someDict2 = \(someDict2.isEmpty)")
print("someDict3 = \(someDict3.isEmpty)")

Convert to Arrays

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

let dictKeys = [Int](someDict.keys)
let dictValues = [String](someDict.values)

print("Print Dictionary Keys") // default sorted values by alphabet

for (key) in dictKeys {
   print("\(key)")
}
print("Print Dictionary Values") // default sorted values by alphabet

for (value) in dictValues {
   print("\(value)")
}

Accessing Dictionaries

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var someVar = someDict[1]

// print( "Value of key = 1 is \(someVar)" )
// print( "Value of key = 2 is \(someDict[2])" )
// print( "Value of key = 3 is \(someDict[3])" )

var studentDict: [String:String] = ["001":"Ca", "002":"Yen", "003":"Khang"]
for item in studentDict {
    print("key: \(item.key) --- value: \(item.value)")
}

// Update
studentDict.updateValue("Pham Gia Khang", forKey: "003")
print("\nDictionary after update!!!\n")
for item in studentDict {
    print("key: \(item.key) --- value: \(item.value)")
}

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

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