0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【SwiftUI】Function is unused

Posted at

やろうとしたこと

  • コンポーネント化したButtonにタップアクションを渡せるようにしようとした

発生したエラー

Function is unused

該当のコード

Comoponent.swift
struct ButtonComponents: View {
    @State var buttonText: String
    var onVoid: () -> Void
    var body: some View {
        Button(action: {
            onVoid // Function is unused
        }) {
            Text(buttonText)
                .bold()
        }
        .frame(maxWidth: .infinity)
        .frame(maxHeight: 48)
        .accentColor(.white)
        .background(Color.getRawColor(hex: "0017C1"))
        .clipShape(RoundedRectangle(cornerRadius: 8))
    }
}

解決策

  • ()は「関数を実行する」という意味があるらしい
  • 上記のコードがエラーを起こしていたのは、シンプルに関数を呼び出すだけ呼び出しといて実行してなかったから。

修正後のコード

Button(action: {
    onVoid()
})
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?