Swift

[Swift]Swift #3

Swift #3

func initUI(){
    //配列-固定
    println( "-------------- array1" )
    let array1 = [0, 3, "あいうえお"]
    println( array1 )
    
    //配列-可変
    println( "-------------- array2" )
    var array2:[Int] = [1,2,3]
    array2[1]=57
    println( array2 )
    
    println( "-------------- array3" )
    var array3:[Float] = [4.0, 768.0, 0.432, 0.2]
    array3[0]=5.0
    array3[2]=346.0
    println( array3 )
    
    println( "-------------- array4" )
    var array4:[String] = ["か","き","く","け"]
    array4[2]="さしすせそ"
    println( array4 )
    
    //Dictionary
    println( "-------------- Dictionary" )
    var dic1:[String:NSObject?] = ["name":"た","address":"ち","number":"つ"];
    println( dic1 )
    println( dic1["address"] )
    dic1["address"]="マダガスカル"
    println( dic1 )
    
    //for in 1
    println( "-------------- for in 1" )
    for value in array2 {
        println( value )
    }
    
    //for in 2
    println( "-------------- for in 2" )
    for obj in dic1 {
        println( obj )
    }
    
    //for in 3
    println( "-------------- for in 3" )
    for i in 1..<8 {
        println( i )
    }
    
    //for in 4
    println( "-------------- for in 4" )
    for i in 1...8 {
        println( i )
    }
    
    //for in 5
    println( "-------------- for in 5" )
    for i in 0..<array3.count {
        println( "\(i) : \(array3[i])" )
    }
    
    //for in 6
    println( "-------------- for in 6" )
    for obj in dic1 {
        var (key,value)=obj
        println( "\(key) : \(value)" )
    }
    
    //for
    println( "-------------- for" )
    for var i=0;i<10;i++ {
        println( "for : \(i)" )
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    initUI()
}

出力結果

-------------- array1
(
    0,
    3,
    "\U3042\U3044\U3046\U3048\U304a"
)
-------------- array2
[1, 57, 3]
-------------- array3
[5.0, 768.0, 346.0, 0.200000002980232]
-------------- array4
[か, き, さしすせそ, け]
-------------- Dictionary
[number: つ, name: た, address: ち]
ち
[number: つ, name: た, address: マダガスカル]
-------------- for in 1
1
57
3
-------------- for in 2
(number, つ)
(name, た)
(address, マダガスカル)
-------------- for in 3
1
2
3
4
5
6
7
-------------- for in 4
1
2
3
4
5
6
7
8
-------------- for in 5
0 : 5.0
1 : 768.0
2 : 346.0
3 : 0.200000002980232
-------------- for in 6
number : つ
name : た
address : マダガスカル
-------------- for
for : 0
for : 1
for : 2
for : 3
for : 4
for : 5
for : 6
for : 7
for : 8
for : 9

[Swift]Swift #2

Swift #2

func initUI()
{
    //返り値
    println( getMessage() )
    
    //引数のデフォルト値
    println( getMessageOfJeff() )
    
    //Tuples(タプル)
    let message = (2014,"what are you talking about ?")
    let (year,question) = message
    println( message )
    println( "西暦\(year)" )
    println( "question:\(question)" )
}
func getMessage()->String{
    return "I am a pen."
}
func getMessageOfJeff(message:String="Is this a pen ?")->String{
    return message;
}

override func viewDidLoad() {
    super.viewDidLoad()
    initUI()
}

出力結果

I am a pen.
Is this a pen ?
(2014, what are you talking about ?)
西暦2014
question:what are you talking about ?

参考
The Swift Programming Language: The Basics

Tuples can return multiple values from a function as a single compound value.

[Swift]Swift #1

Swift #1

func initUI()
{
    //行の終わりに;(セミコロン)はいらない
    //定数
    let message = "Hello,world!"
    println(message)
    
    //変数
    var n = 34
    println("わたしは\(n*2)歳です")
}

override func viewDidLoad() {
    super.viewDidLoad()
    initUI()
}

出力結果

Hello,world!
わたしは68歳です