首页 > 解决方案 > SwiftUI 预览失败并出现 __designTimeString 错误

问题描述

我的应用程序构建成功并且运行良好,但所有预览都不起作用。错误只是说“构建失败”,并且有一个选项可以查看诊断。诊断说明如下:

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

它指向错误源的拖动手势如下:

.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
    }
)

任何想法是什么原因造成的?正如我所说,应用程序功能,但每次我调整 UI 间距或类似的东西时不必重新构建会很好。

标签: swiftswiftui

解决方案


我的问题是相同的。对我来说,原来是.named()坐标空间中的文字字符串。

如果您首先创建字符串

let coordinateSpaceName: String = "BoardCoordinates"

然后使用:

.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
    }
)

这应该确保字符串符合ExpressibleByStringLiteral. 为什么一个字面的不我还没有想通。


推荐阅读