Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Created September 17, 2024 02:25
Show Gist options
  • Save samsonjs/5c728655b877c9d3a0475d04e863ddd3 to your computer and use it in GitHub Desktop.
Save samsonjs/5c728655b877c9d3a0475d04e863ddd3 to your computer and use it in GitHub Desktop.
FB15150266: PhotosPicker label closure issue with Xcode 16.0
import PhotosUI
import SwiftUI
enum SFSymbol: String {
case ant
}
extension Label where Title == Text, Icon == Image {
init(_ titleKey: LocalizedStringKey, systemSymbol: SFSymbol) {
self.init(titleKey, systemImage: systemSymbol.rawValue)
}
}
struct ContentView: View {
@State private var item: PhotosPickerItem?
var body: some View {
VStack {
PhotosPicker(selection: $item) {
Label("hello", systemSymbol: .ant)
}
}
}
}

Basic Information

Please provide a descriptive title for your feedback:

PhotosPicker initializer label closure can't call @MainActor methods

Which platform is most relevant for your report?

iOS

Which technology does your report involve?

SwiftUI

What type of feedback are you reporting?

Incorrect/Unexpected Behavior

What build does the issue occur on?

iOS 18 Seed 8 (22A5350a)

Where does the issue occur?

In Xcode

Description

Please describe the issue and what steps we can take to reproduce it:

When using SwiftUI it's fairly common to have view initializers and modifier methods that are annotated with @MainActor, and these cannot be used from the label closure on the PhotosPicker initializer because it's marked as nonisolated.

Example, distilled from the SFSafeSymbols library which is how I actually encountered this, along with an effect modifier from Pow.

import PhotosUI
import SwiftUI

enum SFSymbol: String {
    case ant
}

extension Label where Title == Text, Icon == Image {
    init(_ titleKey: LocalizedStringKey, systemSymbol: SFSymbol) {
        self.init(titleKey, systemImage: systemSymbol.rawValue)
    }
}

struct ContentView: View {
    @State private var item: PhotosPickerItem?

    var body: some View {
        VStack {
            PhotosPicker(selection: $item) {
                Label("hello", systemSymbol: .ant)
            }
        }
    }
}

Adding @preconcurrency on the imports has no effect. Should we be declaring these initializers as nonisolated? There's no explicit @MainActor here so it seems like PhotosPicker shouldn't have a @Sendable nonisolated closure for its label, though it does work with nonisolated inits on Label directly from SwiftUI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment