Back tick `` keyword escape doesn't work for some reason

Inside an iOS SwiftUI project, It compiles but fail to run with this error:

found an unexpected second identifier in function declaration; is there an accidental break?

extension View {
	@ViewBuilder func `if`<T>(_ condition: Bool, transform: (Self) -> T) -> some View where T : View {
		if condition {
			transform(self)
		} else {
			self
		}
	}
}

This code is from: Conditionally apply modifier in SwiftUI

Same with my little test:

import SwiftUI

enum Bar {
    static func `default`(value: Int) -> Int {
        value + 1
    }
}

struct UserDefaultBackTick: View {
    var body: some View {
        Text("\(Bar.default(value: 100))")
    }
}

struct UserDefaultBackTick_Previews: PreviewProvider {
    static var previews: some View {
        UserDefaultBackTick()
    }
}

swiftc the following is fine:

enum Bar {
    static func `default`(value: Int) -> Int {
        value + 1
    }
}

print(Bar.default(value: 100))

1 Like

I have the same issue.
The code Xcode generates for previews becomes something like this:

@_dynamicReplacement(for: `if`(_:)) private func __preview__`if`( //...

And of course it can't compile the __preview__`if` function name with an unexpected second identifier.

Is there a way to prevent such preview code generation for a specific method, variable, struct, ... etc? Because once in a while I run into a different code generation issue that makes my previews fail to build while the project builds normally.

Worth filing a bug with Apple.

1 Like
Terms of Service

Privacy Policy

Cookie Policy