SwiftUI previews fails with __designTimeString error

Viewed 207

My application builds successfully and runs fine, but none of the previews will work. The error just says "Failed to build" and there's an option to see the diagnostics. The diagnostics say the following:

global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'

----------------------------------------

CompileDylibError: Failed to build GameCellView.swift

Compiling failed: global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'

/Users/Starx/Documents/Code/Swift/Shobu/Shobu/Views/GameCellView.swift:45:49: error: global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'
            DragGesture(coordinateSpace: .named(__designTimeString("#5282.[2].[6].property.[0].[0].arg[0].value.[1].[0].[0].modifier[3].arg[0].value.arg[0].value.arg[0].value", fallback: "BoardCoordinates")))
                                            ^
SwiftUI.__designTimeString:1:13: note: where 'T' = 'AnyHashable'
public func __designTimeString<T>(_ key: String, fallback: T) -> T where T : ExpressibleByStringLiteral

The drag gesture it points to as the source of the error is the following:

.gesture(
  DragGesture(coordinateSpace: .named("BoardCoordinates"))
    .onChanged {point in
      guard CanDrag else {
        return
      }
      dragAmount = CGSize(width: point.translation.width, height: point.translation.height)
      dragState = GetDragState(Point: point.location)
    }
    .onEnded {point in
      CellDropped(Point: point.location)
      dragAmount = .zero
      dragState = .unknown
    }
)

Any ideas what's causing this? As I said, the App functions, but it would be nice to not have to rebuild every time I'm adjusting UI spacing or something like that.

1 Answers

The issue for me was identical. Turned out to be the literal string in the .named() coordinate space for me.

If you create the string first with

let coordinateSpaceName: String = "BoardCoordinates"

and then use:

.gesture(
  DragGesture(coordinateSpace: .named(coordinateSpaceName))
    .onChanged {point in
      guard CanDrag else {
        return
      }
      dragAmount = CGSize(width: point.translation.width, height: point.translation.height)
      dragState = GetDragState(Point: point.location)
    }
    .onEnded {point in
      CellDropped(Point: point.location)
      dragAmount = .zero
      dragState = .unknown
    }
)

that should ensure that string conforms to ExpressibleByStringLiteral. Why a literal one does not I haven't figured out yet.

Related