11
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift メソッドの書き方覚え書き

Last updated at Posted at 2014-06-04

#メソッドの書き方

###引数なし 返り値なし

1
//呼び出し
methodOne()

//メソッド
func methodOne() {
    println("")
}

###引数2つ 返り値1つ
第二引数をいれるときは↓みたいに書く
ないとエラーになるので、参考書間違ってるぽい

2
//呼び出し
var a = doubleParam("なまえ", day:"日付")

//これだとダメだった
var a = doubleParam("なまえ", "日付")

//メソッド
func doubleParam(name: String, day: String) -> String {
    return "\(name)\(day)"
}

###引数なし 返り値3つ

3
//呼び出し 配列で返るよ
var arr = tripleReturn();

func tripleReturn() -> (Int, String, Bool) {
    return (100, "String", true)
}

###引数複数

4
//呼び出し
var sum = sumOf(1,3,4,5,6);
println(sum)

func sumOf(numbers: Int...) -> Int {
     //numbersは配列
     var sum = 0
     for num in numbers {
         sum += num
     }
     return sum
}

###nest

5
//呼び出し
var nst = nest();
println(nst)

func nest() -> Int {
    func getOne() -> Int {
        return 1
    }
    return getOne()
}
11
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?