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

awakeFromNibメソッドで発生するSwift Concurrencyのwarning対処方法

Last updated at Posted at 2024-12-25

該当するWarning

Main actor-isolated property '〜UIKitのプロパティ〜' can not be referenced
from a nonisolated context; this is an error in the Swift 6 language mode

原因

awakeFromNibはNSObjectを継承しており、NSObject は Main Actor に紐付けられていないため

対処法

awakeFromNibはメインスレッドで実行されるので、ワークアラウンドとしてMainActor.assumeIsolatedで対応

override func awakeFromNib() {
    super.awakeFromNib()

    moreButton.setTitle("Buttonタイトル", for: .normal)
    moreButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
}

👇 変更

override func awakeFromNib() {
    super.awakeFromNib()

    // 追加
    MainActor.assumeIsolated {
        moreButton.setTitle("Buttonタイトル", for: .normal)
        moreButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
    }
}

参考

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