@Previewable version problem iOS 18 - 17

Hi, i am currently updating my app in SwiftUI from iOS 16.4 to 18.0, i am getting in xcode these new warning

@State' used inline will not work unless tagged with '@Previewable'

so i fix the problem with the suggestions and add the new tag @Previewable and put the variable in the first line of the preview

#Preview {
    @Previewable @State var enabled: Bool = true
    
    return NotificationLabel(enabled: $enabled, type: "test", icon: "star", title: "test", description: "test")
}

the problem is that my app is available from iOS 16.4 to iOS 18.0 and the preview gives me this error

'Previewable()' is only available in iOS 18.0 or newer

but if i put a versione if at the top i get another error

if #available(iOS 18.0, *){
        @Previewable @State var enabled: Bool = true
@Previewable' items must be at root scope in the preview block

is there a way to silence the warning making a two version preview, one for iOS 18 and one for the previous versions?

Answered by Developer Tools Engineer in 791776022

Hi,

Sorry to hear you are having problems getting @State working in previews. I believe you are hitting a known issue that is being tracked. As a workaround could you try:

@available(iOS 18.0, *)
#Preview {
    @Previewable @State var enabled: Bool = true
    NotificationLabel(enabled: $enabled, type: "test", icon: "star", title: "test", description: "test")
}
Accepted Answer

Hi,

Sorry to hear you are having problems getting @State working in previews. I believe you are hitting a known issue that is being tracked. As a workaround could you try:

@available(iOS 18.0, *)
#Preview {
    @Previewable @State var enabled: Bool = true
    NotificationLabel(enabled: $enabled, type: "test", icon: "star", title: "test", description: "test")
}
@Previewable version problem iOS 18 - 17
 
 
Q