Background:我正在使用核心数据来处理我的应用程序的用户设置的持久存储.我的其中一个设置是Color值,我正在将其转换为存储为由Core Data管理的InitialsImageColor类型对象的属性的RGB值.在Form视图中使用ColorPicker来 Select Color.

Problem:我有一个变量imageColor,我正在视图的init()声明中初始化该变量.编译器抛出一个错误,说"变量‘self.ImageColor’在被初始化之前使用"103.为了清楚起见,以下是相关代码部分及其注释:

struct SettingsView: View {
    // Access the managed object context for saving changes to Core Data
    @Environment(\.managedObjectContext) var moc

    // Load stored InitialsImageColor objects from Core Data
    @FetchRequest(sortDescriptors: []) var colorSetting: FetchedResults<InitialsImageColor>
    
    // A Color variable for the ColorPicker View to bind to
    @State private var imageColor: Color

    // A String variable for the Form View's .searchable modifier to bind to
    @State private var searchText: String = ""
    
    // A Bool variable to change to make the .sheet containing SettingsView to disappear
    @Binding var showSettings: Bool
    
    // An initializer to set the starting value for the ColorPicker View to use
    init() {
        imageColor = Color(                        // This line throws error
            red: colorSetting.first?.r ?? 255.0,   // This line throws error
            green: colorSetting.first?.g ?? 255.0, // This line throws error
            blue: colorSetting.first?.b ?? 255.0,  // This line throws error
            opacity: colorSetting.first?.a ?? 1.0  // This line throws error
        )
    }
  1. 这怎么可能呢?变量的第一次使用是在初始化式本身中.这种错误不应该发生.
  2. 我该怎么做才能修复它?

最初,我试图在它的声明中初始化imageColor,但当然不起作用,因为它使用另一个变量来得出它的值,并且作用域在视图的body之外.我把它放在init()号声明中来解决这个问题,然后就发生了这件事.

推荐答案

这里的代码有几个问题.首先,imageColor有一个@State属性包装器,这意味着您实际上想要这样初始化它:

_imageColor = State(initialValue: Color(red: 1, green: 1, blue: 1) 

不过,你也可以很容易地在宣言中写下:

// A Color variable for the ColorPicker View to bind to
@State private var imageColor = Color(red: 1, green: 1, blue: 1)

因为第二个问题是您期望获取请求在初始化期间发生.事实并非如此.你可以也应该期待SwiftUI视图被多次初始化和销毁,而不是在你的控制之下.考虑到这一原理,您可以看到在初始化时执行FETCH请求是没有意义的.它将在对视图的body%求值时执行.

更好的解决方案可能是将 colored颜色 定义为自定义绑定,而不是单独的状态变量,然后可以将其传递给其他视图:

private var imageColor: Binding<Color> {
    Binding<Color>(
        get: { 
            Color(                        
                red: colorSetting.first?.r ?? 255.0, 
                green: colorSetting.first?.g ?? 255.0,
                blue: colorSetting.first?.b ?? 255.0, 
                opacity: colorSetting.first?.a ?? 1.0 
            )
        },
        set: {
            // Write back to your data object
        }
    )
}

(我在这里留下了你的255个值,因为我刚刚抄袭了问题)

Swift相关问答推荐

是否可以将字典项绑定到文本字段?

在列表项内的NavigationLink中的按钮无法正常工作

如何在枚举有关联数据时使用combined if with case

在Xcode SWIFT中运行用Ionic编写的函数

解码器是否需要具有密钥的外部数据表示?

如何从UIKit中的AttributeContainer获取属性并将其分配给某个变量

我如何在 swift 中的 viewdidload 之前进行委托?

UIImage和UIimage.pngData返回两个不同的图像

如何使用Swift宏和@Observable和@Environment?

如何让默认函数参数引用另一个函数参数?

如何在 macOS/AppKit 的窗口选项卡上接受拖动项目的放置?

Swift-SceneKit-无法从art.scnassets加载.scn文件

按钮图像不会立即刷新

如何使 SwiftUI Picker 换行文本

Vapor 4,如何按外键过滤?

Swift:连接两个数组而不重复共享后缀/前缀

使用 DispatchTime.now() + float 延迟?

自定义 Google 登录按钮 - iOS

更改在变量的 willSet 块中设置的值

使用 JSONEncoder 对类型为 Codable 的变量进行编码