Demo entry 1468491

tuples

   

Submitted by anonymous on Mar 13, 2015 at 21:06
Language: Swift. Code size: 934 Bytes.

//======== Tuples
var str = "Hello"
let num = 1000

var myTuple = (str, num)
var myOtherTuple = (str, num, 12345, "Some text")

//это не кортеж(tuple), а словарь(dictionary)
var states = ["AZ":"Arizona", "CA":"California", "DE": "Delaware", "KY":"Kentucky"]
//но для его обработки мы используем кортеж(tuple)
for (abbr, fullname) in states {
    println("\(abbr) is the abbreviation for \(fullname)")
}
//функция возвращает кортеж
func getCurrentSongAndDuration() -> (String, length:Int){
    return("Moonlight in Vermont", 210)
}
//к конкретному значению кортежа можно обращаться по индексу через точку, либо по имени, если оно задано
let result = getCurrentSongAndDuration()
println("The song is \(result.0) and it's \(result.length) seconds long")
//еще один способ получить тот же рузультат
let (name1, length1) = getCurrentSongAndDuration()
println("The song is \(name1) and it's \(length1) seconds long")

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).