[英]Why does swiftUI give me errors for correct code?
SwiftUI has been giving me errors and refusing to build when I know that the code is correct.当我知道代码是正确的时,SwiftUI 一直给我错误并拒绝构建。 For instance the following code was working all of 15 minutes ago.例如,以下代码在 15 分钟前一直在工作。 I tried to wrap the List
in JumpToBookView
in a VStack
and add a Button
underneath the list, and it started spitting out errors on my Image
in BookRow
.我想换行List
中JumpToBookView
在VStack
并添加一个Button
列表下方,并开始了我的吐出的错误Image
在BookRow
。 This code was compiling and showing in the preview before I added the Button
and VStack
, after I removed them, it still won't let me preview the view.在我添加Button
和VStack
之前,这段代码正在编译并显示在预览中,在我删除它们之后,它仍然不会让我预览视图。 I have tried cleaning the build folder, deleting derived data, and quitting the application, all at the same time, and still nothing.我曾尝试清理构建文件夹、删除派生数据并退出应用程序,所有这些都同时进行,但仍然没有。 No preview, and the project won't compile.没有预览,项目将无法编译。 For background, the project is mainly UIKit, but I'm trying to incorporate SwiftUI into it, even though that shouldn't matter because this view is not called anywhere.对于背景,该项目主要是 UIKit,但我正在尝试将 SwiftUI 合并到其中,即使这无关紧要,因为此视图未在任何地方调用。 I am currently running the latest version of Xcode, and have attached my code and a screenshot of the errors.我目前正在运行最新版本的 Xcode,并附上了我的代码和错误截图。 For search, I will also paste the errors here.对于搜索,我也会在这里粘贴错误。 Missing argument label 'imageName:' in call
, Value of type 'Image' has no member 'resizable'
, and Cannot infer contextual base in reference to member 'fit'
Missing argument label 'imageName:' in call
Value of type 'Image' has no member 'resizable'
,并且Cannot infer contextual base in reference to member 'fit'
import SwiftUI
struct JumpToBookView: View {
var body: some View {
List {
BookRow(selected: true)
BookRow(selected: true)
BookRow(selected: true)
BookRow(selected: true)
BookRow(selected: true)
BookRow(selected: true)
}
}
func testJump()
{
print("test")
}
}
struct BookRow: View {
var selected: Bool
var body: some View {
ZStack {
Rectangle()
.fill(Color(RaiseColor.red))
HStack{
Text("1.2 What is an Ecosystem?")
.font(.title)
.fontWeight(.bold)
.foregroundColor(Color(.white))
Spacer()
Image(selected ? "pauseButton" : "playButton")
.resizable()
.aspectRatio(1, contentMode: .fit)
.frame(maxWidth: 25)
.foregroundColor(Color(.white))
}
.padding(.all)
}
}
}
struct JumpToBookView_Previews: PreviewProvider {
static var previews: some View {
JumpToBookView()
}
}
EDIT: I just want to add that it was also working with RaiseColor.red
before, so there shouldn't be any issues with that custom color.编辑:我只想补充一点,它之前也与RaiseColor.red
一起使用,因此该自定义颜色应该没有任何问题。
The issue ended up being that my codebase had previously defined a class named Image
, so SwiftUI was having issues figuring out which Image
I was trying to reference.问题最终是我的代码库之前定义了一个名为Image
的类,因此 SwiftUI 在确定我试图引用哪个Image
时遇到问题。 Without making other changes to the codebase, this issue was resolved by specifying SwiftUI as the parent of Image
, which can be done by the following SwiftUI.Image(selected ? "pauseButton" : "playButton")"
. This fixes all of the issues I was having, but the best thing to do would be to refactor your codebase in order to not have conflicting names with SwiftUI classes and structs.无需对代码库进行其他更改,通过将 SwiftUI 指定为Image
的父级来解决此问题,这可以通过以下SwiftUI.Image(selected ? "pauseButton" : "playButton")"
。这解决了所有问题我有,但最好的办法是重构你的代码库,以免与 SwiftUI 类和结构有冲突的名称。
When I replace this当我更换这个
Rectangle() .fill(Color(RaiseColor.red))
with this有了这个
Rectangle()
.fill(Color.red)
all compiled well... so look for error in that custom color.一切都编译得很好......所以在那个自定义颜色中寻找错误。
My issue was that i named an intentdefinition enum to Color.我的问题是我将一个意图定义枚举命名为 Color。 I renamed it and it's working fine again.我重命名了它,它再次正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.