1
2

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でC言語みたいな関数ポインタ呼び出しを使いたいんですけど

1
Posted at

Oさんからの質問です。

Q.
swift3 で C でいうところの関数ポインタ呼び出しをしたいのですが可能でしょうか?
そもそも不可能だったりしますか?
以下擬似コード

q.swift
var _action //こんな変数を用意する
func foo( action: ( ( Void ) -> Void )? = nil )
{
    _action = action // 別変数に代入する
}
func bar()
{
    _action() // こんな感じで呼び出す
}

知っている方教えてくださいませ!

A.
こんな感じですかねぇ。
以下、プレイグラウンド用。

a.swift
var _action :  ( ( Void ) -> Void )?

func hoge() {
    print("hello")
}


func foo( action: ( ( Void ) -> Void )? = nil )
{
    _action = action
}


func bar() {
    _action?()
}

foo(action: hoge);
bar();

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?