Xcode 4中的核心数据获取请求变量

smo*_*ter 10 core-data ios

如何在Xcode 4的核心数据中为变量请求添加变量?找不到它.

ovi*_*diu 31

Expression从下拉列表中选择并使用$替换变量前面的表达式输入表达式(NAME在下面的示例中).即使替换变量的值是一个字符串,也要确保不要将变量放在引号之间,否则替换将不起作用.

使用替换变量获取谓词

在您的代码中,您可以像这样引用fetch谓词(XCode 4.4及更高版本):

NSManagedObjectModel* model = [[context persistentStoreCoordinator] managedObjectModel];
NSFetchRequest* request = [model fetchRequestFromTemplateWithName:templateName
                                            substitutionVariables:@{@"NAME" : name}];
NSError* error = nil;
NSArray* results = [context executeFetchRequest:request error:&error];
Run Code Online (Sandbox Code Playgroud)

  • 在 Xcode 9 中,您需要选择 Custom Predicate 而不是 Expression (2认同)

Dar*_*ers 6

Apple已经在最新的XCode中重新推出了它.要在Core Data编辑器的Fetch Requests中使用Variables,您必须使用"expression"类型并手动输入:

data == $ DATA

(在此表达式中,$ DATA是变量).