swift - 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 间距或类似的东西时不必重新构建会很好。
解决方案
我的问题是相同的。对我来说,原来是.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
. 为什么一个字面的不我还没有想通。
推荐阅读
- javascript - 反应视频播放器不在打字稿上播放本地视频
- python - 如何在 django 中一次创建两个模型
- powershell - powershell:将加密的用户密码保存在文件中以进行比较
- string - 如何在 GitHub 操作中以 json 格式发送多行字符串?
- c++ - 是否可以让 boost multi_index 容器索引一个具有 2 个键值的元素?
- postgresql - 将两个 Docker Container Flask 连接到 PostGIS
- reactjs - 如何去掉ant design的Modal上的滚动条?
- ios - 通过 TextField 定义变量然后将其传递给 Simple Swift UI 应用程序中的观察对象的问题
- python - Matplotlib FuncAnimation 视频质量差
- ios - 在 viewContext 上使用 perform 方法和不使用 perform 方法有什么区别?- 核心数据