What CoreData fetch request is actually returned?

I retrieve some objects from the data store, but the results are not what I expect. I'm new to CoreData, but I'm sure this should work. What am I missing?

Note that the User is a valid managed object and that I am including its header file in this code and that UserID is a valid property of this 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
}

Not fetch the results of an array of user objects? Will [fetchResults objectAtIndex: i] not be a user object? Why does an error occur when creating this " query for the UserID member in something other than a structure or union "?

, , . , , . ( , , fetchResults Objective C, , , .)


:

( )

- , saveAction . KVC ? :

for (User thisUser in fetchResults) {

... .

:

(id thisUser in fetchResults)

...

[thisUser valueForKey:@"FirstName"] = anything

... Lvalue.

:

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

... ? , , , fetchResults.

+3
3

fetchedResults NSArray. NSArray . C, , NSArray .

, :

[fetchResults objectAtIndex:i].UserID =

... , , , , [fetchResults objectAtIndex:i]. , , UserID. "request for member 'UserID' in something not a structure or union". , [fetchResults objectAtIndex:i] - , , "UserID".

, , . . .

NSManagedObject , NSManagedObject associative storage, NSManagedObject , . , , . NSMangedObjects setValue:forKey:/valueForKey:, objectInstance.propertyName.

, NSManagedObject. NSManagedObject :

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

... NSManageObject, User, . ( , ):

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

- NSManagedObject. ( Cocoa , .)

NSManagedObject , , . , NSManagedObject. Xcode , :

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

. , :

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

, fetchedResults . , userID . NSManagedObject, :

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

, :

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

(: NSManagedObject.)

+8

CoreData Accessors ( ) , NSManagedObject . @dynamic. :

//Assume this exists:

@interface User : NSManagesObject 
{
}
@property (nonatomic, retain) NSString* UserID;

@end

@implementation User

@dynamic UserID

@end

// You could do:

for (int i; i < [fetchResults count]; i++) {
    ((User*)[fetchResults objectAtIndex:i]).UserID = ... // This works
}

KVC , ( ):

for (int i; i < [fetchResults count]; i++) {
    [[fetchResults objectAtIndex:i] valueForKey:@"UserID"] = ... // This too
}

[object setValue:newValue forKey:@"UserID"], , newValue NSString, NSNumber, NSDate, NSSet CoreData.

:

:

for (id object in fetchResults) {
    [object valueForKey:@"UserID"] = ...
}

ANY . "UserID IN% @" .

+2

, -objectAtIndex: returtns id. id , , -objectAtIndex:, , C. id - , , , , .

- . , NSObject, .

:

  • for (User* aUser in theArray)
    {
        ....
    }
    

    ,

  • -objectAtIndex: .

    ((User*)[theArray objectAtIndex: i]).userId;
    
  •   [[theArray objectAtIndex: i] setUserId: ...];
    

1 3.

for (User* aUser in theArray)
{
   [aUser setUserId: ...]
}

Obviously, any of the above dangers, if you are not sure that the objects in the array are User objects. You can use -respondsToSelector:it to make sure it will work if you want.

+2
source

Source: https://habr.com/ru/post/1760026/


All Articles