Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7491509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:04:04+00:00 2026-05-29T16:04:04+00:00

I am trying to write a method which tries to retrieve an object based

  • 0

I am trying to write a method which tries to retrieve an object based on a predicate using a variable (as part of a NSXMLParser). The code looks like this:

I have these variables defined in the class:

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@property (strong, nonatomic) NSString *model;
@property (strong, nonatomic) NSString *element;

Now in the method, I set up the request like this:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:self.model inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];

Now the challenge – what I want to be able to do is:

// DOES NOT WORK
[request setPredicate:[NSPredicate predicateWithFormat:@"%@ == %@", self.element,string]];

But that does not return any results. After some mucking around, I notice that this does work:

 if ( [self.element isEqualToString:@"name"] ) {
     [request setPredicate:[NSPredicate predicateWithFormat:@"name == %@", string]];
 }

This tells me that my self.element is set correctly (I think?) but that the predicate doesn’t like the left hand side of the expression being a variable.

I also tried:

[request setPredicate:[NSPredicate predicateWithFormat:@"%s == %@", [self.element UTF8String],string]];

… just to see if perhaps it preferred a string. I couldn’t make that work either.

Is what I am attempting even possible? I’ve read as much as I can of the Core Data documentation and I can’t find any sample code which does it this way, but I also didn’t find anything to say it wasn’t possible.

EDIT:
and now the working code:

[request setPredicate:[NSPredicate predicateWithFormat:@"%K == %@", self.element,string]];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:04:04+00:00Added an answer on May 29, 2026 at 4:04 pm

    If you haven’t done before, read the Predicate Programming Guide and you can find everything about predicates.

    Now, if I’ve understood correctly, you want to create a predicate between two string but the first isn’t a defined string but it can change.
    I haven’t used them before but I believe you can solve your problem by using a predicate with dynamic property names.

    Here from the documentation:

    Dynamic Property Names
    Because string variables are surrounded by
    quotation marks when they are substituted into a format string using
    %@, you cannot use %@ to specify a dynamic property name—as
    illustrated in the following example.

    NSString *attributeName = @"firstName";  
    NSString *attributeValue = @"Adam";  
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ like %@", attributeName, attributeValue];  
    

    The predicate format string in this case evaluates to “firstName” like “Adam”.

    If you want to specify a dynamic property name, you use %K in the
    format string, as shown in the following fragment.

    predicate = [NSPredicate predicateWithFormat:@"%K like %@", attributeName, attributeValue];  
    

    The predicate format string in this case evaluates to firstName like “Adam” (note that there are no
    quotation marks around firstName).

    More info can be found also here: Predicate Format String Summary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a method which will return me a code corresponding
I am trying to write a unit test for an action method which calls
I'm trying to write some C# code that calls a method from an unmanaged
I'm trying to write a java method which finds all the modes in an
I am trying to write a unit test for a method which takes an
Hey, I'm trying to write a loop which updates within a method at 1/60
I'm trying to write a regex or Ruby method which will find the longest
I'm trying to write some code which shares a page on Facebook and twitter.
I'm trying to write a wrapper class for a protected method which I wish
I'm trying to write a method like this: public static T Test<T>() { if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.