14
14

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] extensionで機能拡張

Posted at

Swiftではextensionを使ってクラスの機能拡張が行えます。
Objective-Cで言うところの拡張カテゴリですね。

ってことでサンプルコード。

class Hoge {
	var name: String?
	init() {

	}
	func test() -> String {
		return "hoge"
	}
}

extension Hoge {
	// extensionで実装を差し替えられる
	func test() -> String {
		return "fuga"
	}

	// オーバーロードもできる
	func test(a: Int) -> Int {
		return a + 1
	}
}

var hoge: Hoeg = Hoge()
println(hoge.test())  // => "fuga"
println(hoge.test(3)) // => 4
14
14
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
14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?