在@FetchRequest中输入动态值,以从SwiftUI中的核心数据中获取单个实体 [英] Input a dynamic value into @FetchRequest, to fetch a single entity from core data in SwiftUI

查看:141
本文介绍了在@FetchRequest中输入动态值,以从SwiftUI中的核心数据中获取单个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了相同类型的错误,但是这里使用的是不同类型的代码,所以我认为最好在这种情况下提出一个新问题。我试图通过尝试将名为 title 的字符串变量(用作查找该实体的键)从核心数据中找到特定实体, $ c> @FetchRequest 。这是我使用的代码的一部分

I saw same type of error but with different kind of code here, so I think it's better to ask a new question on this context. I have attempted to "find a specific entity" from core data by trying to pass a string variable (which use as a key to find that entity) called title into @FetchRequest. This is part of the code I have used

struct AccountMainPage: View {
    //*** User input ***
    var title: String

    //*** Core data enviroment initialisation ***
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(fetchRequest: Accounts.getSpecificAccounts(findTitle: title)) var fetchedAccount: FetchedResults<Accounts>


    var body: some View {
    //SOME CODE HERE
    }
}

公共类帐户具有扩展名:

extension Accounts {
    static func getSpecificAccounts(findTitle: String) -> NSFetchRequest<Accounts> {
    let request: NSFetchRequest<Accounts> = Accounts.fetchRequest() as! NSFetchRequest<Accounts>

    let findDescriptor = NSPredicate(format: "title == %@",findTitle)

    request.predicate = findDescriptor

    return request
    }
}

但是,与 @FetchRequest的行(fetchRequest:Accounts.getSpecificAccounts(findTitle:title))var fetchedAccount:FetchedResults< Accounts> 有语法错误:


不能在属性初始化程序中使用实例成员 title;属性初始值设定项在'self'可用之前运行

Cannot use instance member 'title' within property initializer; property initializers run before 'self' is available

我的代码有问题吗?

推荐答案

@FetchRequest 是动态属性,它像其他任何属性一样在之前初始化。 AccountMainPage init 被调用,因此 self 尚不可用,这就是为什么您不能使用 title 属性,该属性是 self 的成员,这与编译器错误说明有关。

@FetchRequest is dynamic property which is initialised, as any other property, before your AccountMainPage init is called, so self is not available yet, that is why you cannot use title property which is a member of self, and that is about what compiler error tells.

所以这是一个可能的解决方案:我们用存根请求初始化获取请求属性,然后在 init 中初始化,稍后将其重新初始化

So here is a possible solution: we initialise fetch request property with stub request and then in init, which is called later, reinitialise it with real fetch request.

这是一个方法演示(所有不相关的内容都减少了):

Here is an approach demo (all unrelated things cut):

struct ContentView: View {
    var title: String

    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(fetchRequest: Accounts.fetchRequest()) var fetchedAccount: FetchedResults<Accounts>

    init(title: String) {
        self.title = title
        _fetchedAccount = FetchRequest<Accounts>(fetchRequest: Accounts.getSpecificAccounts(findTitle: title))
    }

...

这篇关于在@FetchRequest中输入动态值,以从SwiftUI中的核心数据中获取单个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆