使用存储在 CoreData 模型中的 FetchRequest 进行变量替换
我总是完全在代码中创建 NSFetchRequests。现在我正在查看 Xcode GUI 来构建获取请求并将其存储在模型中。
我正在遵循 Xcode 文档中的示例。我向我的模型添加了一个 Fetch Request,通过建模 GUI 创建的谓词是:
firstName LIKE[c] "*SUBSTRING*"
然后我用这两行检索该请求:
NSDictionary *substituionDictionary = [NSDictionary dictionaryWithObject:@"woody" forKey:@"SUBSTRING"];
NSFetchRequest *fetchRequest = [mom fetchRequestFromTemplateWithName:@"firstNameContains" substitutionVariables:substituionDictionary];
生成的 NSFetchRequest 的 NSLog 输出如下:
(entity: Customer; predicate: (firstName LIKE[c] "*SUBSTRING*"); sortDescriptors: (null); limit: 0)
.. 这表明该变量不是在存储的 FetchRequest 返回之前被替换。
那么,如何指定在 Xcode 数据建模 Fetch Request Predicate Builder GUI 中输入的文本在运行时被 NSFetchRequest:fetchRequestFromTemplateWithName:substitutionVariables: 替换?
谢谢你!
伍迪
I've always created my NSFetchRequests entirely in-code. Now I'm looking at the Xcode GUI for building a fetch request and storing it in the model.
I'm following an example from the Xcode Documentation. I added a Fetch Request to my model, and the predicate that has been created through the Modelling GUI is:
firstName LIKE[c] "*SUBSTRING*"
I then retrieve that request with these two lines:
NSDictionary *substituionDictionary = [NSDictionary dictionaryWithObject:@"woody" forKey:@"SUBSTRING"];
NSFetchRequest *fetchRequest = [mom fetchRequestFromTemplateWithName:@"firstNameContains" substitutionVariables:substituionDictionary];
An NSLog of the resulting NSFetchRequest outputs this:
(entity: Customer; predicate: (firstName LIKE[c] "*SUBSTRING*"); sortDescriptors: (null); limit: 0)
.. which indicates that the variable is not being substituted prior to the return of the stored FetchRequest.
So, how does one specify that text entered in the Xcode Data Modelling Fetch Request Predicate Builder GUI is intended to be substituted at runtime by NSFetchRequest:fetchRequestFromTemplateWithName:substitutionVariables: ?
Thank you!
Woody
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要右键单击包含预期变量的获取请求谓词编辑器的行,然后从弹出窗口中选择“VARIABLE”。在 Xcode 编辑器中右键单击的位置有时很挑剔,因此我倾向于单击 +/- 按钮的左侧。
You need to right-click on the row of the fetch request predicate editor containing the intended variable and select "VARIABLE" from the popup. Where you right-click is sometimes picky in the Xcode editor, so I tend to click just to the left of the +/- buttons.
这是替换变量的示例。
首先在获取请求部分创建 fetchRequest 模板。
然后编写通过名字获取员工的代码。
Here is the example of substitution Variables.
First create the fetchRequest Template in the Fetch Requests Section.
Then write the code for fetch employee by firstName.