Xcode 4: Fetch Request Template Variables?

霸气de小男生 提交于 2019-12-03 09:01:01

问题


In Xcode 3.X, you were supposed to right-click the whitespace in the fetch request template's predicate editor to specify a variable input rather than a hard-coded predicate.

Where is this in XCode 4? I've held option, right-clicked, option-clicked, etc and cannot figure it out....


回答1:


I don't think X4 has the variable anymore.

Instead, I think you have to choose an expression and then provide a variable of the form $VARNAME.

For example, given and entity Alpha with an attribute aString, I created a fetch request template bobFetch with an expression of aString == $TESTVAR.

Alpha *a=[NSEntityDescription insertNewObjectForEntityForName:@"Alpha" inManagedObjectContext:self.moc];
a.aString=@"steve";
[self saveContext];
NSDictionary *subVars=[NSDictionary dictionaryWithObject:@"steve" forKey:@"TESTVAR"];
NSFetchRequest *fetchRequest =   [self.managedObjectModel fetchRequestFromTemplateWithName:@"bobRequest" substitutionVariables:subVars];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Alpha" inManagedObjectContext:self.moc];
[fetchRequest setEntity:entity];

If logged fetchRequest reports:

<NSFetchRequest: 0x4d17480> (entity: Alpha; predicate: (aString == "steve"); sortDescriptors: ((null)); type: NSManagedObjectResultType; )

... and can then be used normally.

NSError *error = nil;
NSArray *fetchedObjects = [self.moc executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
    NSLog(@"fetch error = %@",error);
}
NSLog(@"fetchObjects = %@",fetchedObjects);

Kind of clumsy for a graphical environment but it works.




回答2:


This has been tweaked in Xcode 4. In order to use substitution variables, you need to choose "Expression" from the popup menu (i.e. instead of an attribute name) and you can enter the equivalent like this: name == $SEARCH_NAME

If you were to just enter a $VARIABLE value in the field for each attribute, you'll get the wrong result. In fact, some attributes won't allow that such as Date attributes where you are forced to enter a value.

Of course you can use multiple variables from there on.

Then it's just as before with executing the fetch request:

NSString *searchName = @"Mr Squiggle";
NSDictionary *subs = [NSDictionary dictionaryWithObject:searchName forKey:@"SEARCH_NAME"];
NSManagedObjectModel *model = [self managedObjectModel];
NSFetchRequest *req = [model fetchRequestFromTemplateWithName:@"trainerByName" substitutionVariables:subs];
NSError *error = nil;
NSArray *results = [[self managedObjectContext] executeFetchRequest:req error:&error];
NSLog(@"Found %ld record.", [results count]);

Note you can also do away with the attributes popup and just click the button on the top right of the editor (looks like lines right beside the default grid view button) and just enter your expression straight away. This is a good way of seeing how some things like dates get translated.



来源:https://stackoverflow.com/questions/7099595/xcode-4-fetch-request-template-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!