Tutorialspoint

Swift Programming Examples

Compile and Execute Swift Online

import Foundation


var Nummer = [Int]()
var Pokedex = [String]()
var Deutsch = [String]()
var Englisch = [String]()
var Legendaer = [String]()
var Speziell = [String]()
var Sagenumwoben = [String]()let
var Mysterioes = [String]()
var Normal = [String]()
var Feuer = [String]()
var Wasser = [String]()
var Elektro = [String]()
var Pflanze = [String]()
var Flug = [String]()
var Kaefer = [String]()
var Gift = [String]()
var Gestein = [String]()
var Boden = [String]()
var Kampf = [String]()
var Eis = [String]()
var Psycho = [String]()
var Geist = [String]()
var Drache = [String]()
var Unlicht = [String]()
var Stahl = [String]()
var Fee = [String]()
var Datensaetze = 0

Deutsch.append("Pokemon")
/*

for i in 1...812 {
    let input = readLine()
    
    
    if let Datensatz = input {
        
        let DatensatzArr = Datensatz.split(separator: " ")
        
        Nummer.append(i)
        Pokedex.append(String(DatensatzArr[0]))
        Deutsch.append(String(DatensatzArr[1]))
        Englisch.append(String(DatensatzArr[2]))
        Legendaer.append(String(DatensatzArr[3]))
        Speziell.append(String(DatensatzArr[4]))
        Sagenumwoben.append(String(DatensatzArr[5]))
        Mysterioes.append(String(DatensatzArr[6]))
        Normal.append(String(DatensatzArr[7]))
        Feuer.append(String(DatensatzArr[8]))
        Wasser.append(String(DatensatzArr[9]))
        Elektro.append(String(DatensatzArr[10]))
        Pflanze.append(String(DatensatzArr[11]))
        Flug.append(String(DatensatzArr[12]))
        Kaefer.append(String(DatensatzArr[13]))
        Gift.append(String(DatensatzArr[14]))
        Gestein.append(String(DatensatzArr[15]))
        Boden.append(String(DatensatzArr[16]))
        Kampf.append(String(DatensatzArr[17]))
        Eis.append(String(DatensatzArr[18]))
        Psycho.append(String(DatensatzArr[19]))
        Geist.append(String(DatensatzArr[20]))
        Drache.append(String(DatensatzArr[21]))
        Unlicht.append(String(DatensatzArr[22]))
        Stahl.append(String(DatensatzArr[23]))
        Fee.append(String(DatensatzArr[24]))
    
        
        
    }
    else {
        
        print("end of File reached")
        break
    }



}

*/

print(Deutsch[(0)])
print(Englisch[(0)])

Compile and Execute Swift Online

import Foundation
import Glibc
 
var buffered_data: [String: Any] = [:]
var dictArray:[Int] = [] 
buffered_data.updateValue(0, forKey: "t")
            
buffered_data.updateValue(4444, forKey: String(4))
buffered_data.updateValue(2222, forKey: String(2))
buffered_data.updateValue(5555, forKey: String(5))

var sortedByKeyDictionary = buffered_data.sorted { firstDictionary, secondDictionary in
    return firstDictionary.0 < secondDictionary.0 // 由小到大排序
}

print(sortedByKeyDictionary.popLast()!)

print(buffered_data)
print(sortedByKeyDictionary)


for (k, v) in sortedByKeyDictionary {
    dictArray.append(v as! Int )
}
print(dictArray)
var a = ["1","2","3"]
if let index = a.index(of: "2") {
    a.remove(at: index)
}
print(a)

Compile and Execute Swift Online

import Foundation
import Glibc


print ("noch was neues")
var vielleicht:Int?
vielleicht = 22
print (vielleicht)


Compile and Execute Swift Online

import Foundation
import Glibc

extension Data {
    func subdata(in range: ClosedRange<Index>) -> Data {
        return subdata(in: range.lowerBound ..< range.upperBound + 1)
    }
}

var test = "122"
var test_int16 = Int16(test)
var data_byte = Data(buffer: UnsafeMutableBufferPointer(start: &test, count: 1))

print("data_byte: ",data_byte)

// var data_test_byte = data_byte.subdata(in:0...1)
// print("data_test_byte: ",data_test_byte)

// var data = Data(buffer: UnsafeMutableBufferPointer(start: &data_byte[0], count: 2))

// data_test_byte = data.subdata(in:0...1)
// print("data_byte: ",Int16(data_test_byte))

let number = data_byte.withUnsafeBytes {
                            (pointer: UnsafePointer<Int8>) -> Int8 in
    return pointer.pointee
}

print(number)


// print("data_byte: ",Int8(data_test_byte[0]))
// print("data_byte: ",data_test_byte[1])


// var index = Int8(0)
// //var data_time = dataTimeFormat(timeStr: dataTime)
// var info = Data(buffer: UnsafeMutableBufferPointer(start: &index, count: 0))

// var data1 = Int8(1)
// let data1_byte = Data(buffer: UnsafeMutableBufferPointer(start: &data1, count: 1))

// var data2 = Int8(1)
// let data2_byte = Data(buffer: UnsafeMutableBufferPointer(start: &data2, count: 1))

// info.append(contentsOf: data1_byte)
// info.append(contentsOf: data2_byte)
// info.append(contentsOf: data2_byte)
// info.append(contentsOf: data2_byte)

// print("info: ",info.subdata(in:0...2))


// var a = Double(-0.75)
// // var b = Int16(a*100)

// let acc_int16_byte = Data(buffer: UnsafeMutableBufferPointer(start: &a, count: 1))
// // print("Acc byte: ",acc_int16_byte)
// info.append(contentsOf: acc_int16_byte)
    
            
// if info.count != 0{
//     var data_byte = start_int8_byte
// var data = Data(buffer: UnsafeMutableBufferPointer(start: &acc_int16_byte, count: 1))
// print("data_byte: ",acc_int16_byte)
// print("data: ",data)
    
//     let a = Int8(data[0])
//     print("a: ",a)
    
// }

april 10 Q

public class House
{
    public var occupant : Int = 1
}

public class HouseStruct
{
    public var occupant : Int = 1
}

func kickOut( house: House) 
{
    house.occupant -= 1
}

func kickOut(house: inout HouseStruct) 
{
    house.occupant -= 1
}

first and cute swift program

let cake=5 
print("I have \(cake) cakes") 
var list=["Biscuit","cherry","lemons"] 
print("we have \(list)") 
var jobs=[
    "manoj": "Engineer", 
    "Manish":"dancer" 
    ]
print("😎  I know some people .... they're \(jobs)")

Adding to Arrays - SPECIAL

var numbers = [10, 20, 30]
numbers += [40]

print(numbers)

var pets = ["Cat", "Dog", "Parrot"]
pets += ["Badger","Ferrit"]

print(pets)

Session 13 Tasks

import Foundation
import Glibc

//Task 1
func applyKTimes(_ K: Int, _ closure: () -> ()){
    for _ in 1...K {
        closure()
    }
}
applyKTimes(3) { 
print("Hello Swift")
}


//Task 2

var numbers = [1, 2, 3, 4, 6, 8, 9, 3, 12, 11]
var result = numbers.filter{$0 % 3 == 0}
print(result)

//OR
// func filter(_ Number: [Int], _ closure: (Int) -> (Bool)) -> [Int]{
// var multiples: [Int] = []
//     for i in Number {
//         if closure(i) {
//           multiples.append(i)
//         }
//     }
//     return multiples
// }   
// var result = filter([3, 6, 9, 3, 12], { $0 % 3 == 0}) 
// print(result)


//Task 3

var numbers2 = [4, 7, 1, 9, 6, 5, 6, 9]
numbers2.sort{$0 > $1}
print(numbers2[0])

//OR
// var numbers2 = [4, 7, 1, 9, 6, 5, 6, 9]
// let max = numbers2.reduce(numbers2[0]){
//     if $0 > $1 {
//         return $0
//     }else{
//         return $1
//     }
// }
// print(max)


//Task 4

var strings = ["We", "Love", "iOS"]
let string =  strings.reduce(""){
    if $0 != "" {
    return $0 + " " + $1
    }else{
        return $1
    }
}
print(string)


//Task 5
var nums = [1, 2, 3, 4, 5, 6]
func countDivisors(_ num: Int) -> Int {
        var count = 0
        for i in 1...num {
            if num % i == 0 {
                count += 1
            }
        }
        return count
    }
nums.sort{countDivisors($0) < countDivisors($1)}
print(nums)


//Task 6
var numberss = [1, 2, 3, 4, 5, 6]
let sum = numberss.filter{$0 % 2 != 0}.map{$0 * $0}.reduce(0){$0 + $1}
print(sum)


//Task 7
func forEach(_ array: [Int], _ closure: (Int) -> ()) {
    for num in array {
        closure(num)
    }
}
forEach([1, 2, 3, 4]) {
print($0 + 1)
}

//Task 8
func combineArrays(_ array1: [Int], _ array2: [Int], _ closure: (Int,Int) -> Int) -> [Int]{
var newArray: [Int] = []
for i in 0..<array1.count{
    newArray.append(closure(array1[i], array2[i]))
}
 return newArray   
}
var resultt = combineArrays([1,2,3,4], [5,5,5,3]) {
$0 * $1
}
print(resultt)

ircmd

import Foundation
var strCommand = "1,2,123456799,10,65535,65536"
var cmd_int:[Int] = []
var cmd_int_16:[UInt16] = []
var ir_Cmd = strCommand.components(separatedBy:",")

if ir_Cmd.count > 0 {
    cmd_int = ir_Cmd.map{Int($0) ?? 0}
    cmd_int_16 = cmd_int.map{UInt16(littleEndian: UInt16(truncatingIfNeeded:$0))}
}

print(ir_Cmd)
print(cmd_int)
print(cmd_int_16)

Session 12

import Foundation
import Glibc

/*
var name: String?
/*
//-Force unwrapp
print(name!)
*/

/*
//-Optional Binding
if let myname = name{
    print(myname)
}
*/

/*
//-Defult Value
print(name ?? "t")
*/

var test: String?
test = "123"

//1St  (Forced Unwrapping)
print(test!)

//2nd Optional Binding
if let tes = test {
    print(tes)
}

//3rd Default
print(test ?? "t")
*/

//Guard
func test2() {
let ss: String?
ss = "sdd"
    guard let num = ss else {
    print("Error")
    return
    }
    print("ss!")
}
test2()






1 2 3 4 5 6 7 ... 61 Next
Advertisements
Loading...

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