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 1011579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:15:19+00:00 2026-05-16T09:15:19+00:00

I’m fetching some objects out of a data store but the results aren’t what

  • 0

I’m fetching some objects out of a data store but the results aren’t what I’m expecting. I’m new to CoreData but I’m fairly certain this should work. What am I missing?

Note that User is a valid managed object and that I include its header file in this code, and that UserID is a valid property of that class.

NSFetchRequest *requestLocal = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:messageManagedObjectContext];
[requestLocal setEntity:entity];
// Set the predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY UserID IN %@", userList];
[requestLocal setPredicate:predicate];
// Set the sorting
... sorting details removed but exist and are fine ...
// Request the data
NSArray *fetchResults = [messageManagedObjectContext executeFetchRequest:requestLocal error:&error];
[requestLocal release];

for (int i; i < [fetchResults count]; i++) {
    [fetchResults objectAtIndex:i].UserID = ...<----HERE
}

Isn’t fetchResults an array of User objects? Wouldn’t [fetchResults objectAtIndex:i] be a User object? Why do I get an error when building that “request for member ‘UserID’ in something not a structure or union“?

Sorry if this is a basic error, I’m clearly missing some basic concept. I’ve done a ton of searching and it seems like it should be right. (I also tried fast enumeration but it complained that fetchResults items weren’t valid Objective C objects, effectively the same error, I think.)


Update:

(from comment below)

My goal is to update the object, calling saveAction after changing it.
Does the KVC method still refer to the actual object? I tried fast enumeration with:

for (User thisUser in fetchResults) {

… but it didn’t like that.

I used the more generic version:

(id thisUser in fetchResults)

…but it won’t let me set

[thisUser valueForKey:@"FirstName"] = anything

… insisting that there’s no Lvalue.

Will:

[[thisUser valueForKey:@"FirstName"] stringWithString:@"Bob"]

… do the trick or is there a better way? Sorry, I know it’s nearly a new question, but I still don’t get what is in the fetchResults array.

  • 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-16T09:15:20+00:00Added an answer on May 16, 2026 at 9:15 am

    Your fetchedResults variable contains a NSArray object. However, a NSArray can hold any arbitrary group of objects. Unlike a standard C array, there is no requirement that the NSArray objects all be of a single class.

    The dot notation you are using here:

    [fetchResults objectAtIndex:i].UserID =
    

    … while a legal syntax, nevertheless confuses the compiler because the compiler has no idea what class of object is returned by [fetchResults objectAtIndex:i]. Without knowing the class it has no idea what the heck UserID is. Hence the error "request for member 'UserID' in something not a structure or union". At the very least you have to cast the return of [fetchResults objectAtIndex:i] to some class so that the complier has a clue as to what ‘UserID’ is.

    However, you simply shouldn’t use this construction even though it legal because it is dangerous. See below for the best practice form.

    Understanding NSManagedObject and its subclasses can be tricky because NSManagedObject itself uses a trick called associative storage which allows any generic NSManagedObject instances to store any property of any entity defined in any model. This can confuse novices because there are multiple ways to refer to the same entities, instances and properties. Sometimes the examples use generic NSMangedObjects and setValue:forKey:/valueForKey: and other times they use objectInstance.propertyName.

    Associative storage works like a dictionary attached to every instance of the NSManagedObject class. When you insert a generic NSManagedObject like this:

    NSManagedObject *mo=[NSEntityDescription insertNewObjectForEntityForName:@"User" 
                                                      inManagedObjectContext:self.managedObjectContext];
    

    … you get an instance of the NSManageObject class whose associative storage keys are set to the properties of the User entity as defined in your data model. You can then set and retrieve the values using key-value coding (which has the same syntax as dictionaries) thusly:

    [mo setValue:@"userid0001" forKey:@"UserID"];
    NSString *aUserID=[mo valueForKey:@"UserID"];
    

    Associative storage allows you represent any complex data model in code without having to write any custom NSManagedObject subclasses. (In Cocoa, it allows you to use bindings which let you create entire programs without writing any data management code at all.)

    However, the generic NSManagedObject class is little better than a glorified dictionary whose saving and reading is handled automatically. If you need data objects with customized behaviors you need to explicitly define a NSManagedObject subclass. If you let Xcode generate the class from the entity in the data model you end up with a source file something like:

    User.h
    @interface User :  NSManagedObject  
    {
    }
    
    @property (nonatomic, retain) NSString * firstName;
    @property (nonatomic, retain) NSString * userID;
    @property (nonatomic, retain) NSString * lastName;
    
    @end
    
    User.m
    #import "User.h"
    
    
    @implementation User 
    
    @dynamic firstName;
    @dynamic userID;
    @dynamic lastName;
    
    @end
    

    Now, you are no longer limited by to the key-value syntax of associative storage. You can use the dot syntax because the complier has a class to refer to:

    User *aUser=[NSEntityDescription insertNewObjectForEntityForName:@"User" 
                                                      inManagedObjectContext:self.managedObjectContext];
    aUser.userID=@"userID0001";
    NSString *aUserID=aUser.userID;
    

    With all this in mind, the proper forms of reference to the fetchedResults array become clear. Suppose you want to set all userID properties to a single default value. If you use the generic NSManagedObject class you use:

    for (NSManagedObject *aMO in fetchedResults) {
        [aMO setValue:@"userid0001" forKey:@"UserID"];
        NSString *aUserID=[aMO valueForKey:@"UserID"];
    }
    

    If you use a dedicated subclass you would use:

    for (User *aUserin fetchedResults) {
        aUser.userID=@"userID0001";
        NSString *aUserID=aUser.userID;
    }
    

    (Note: you can always use the generic form for all NSManagedObject subclasses as well.)

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I want to construct a data frame in an Rcpp function, but when I
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words

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.