Programming in objective-c: objective c – Core Data: Unable to call private method on a NSManagedObject subclass on newest questions tagged objective-c – Stack Overflow

I have a problem with the Core Data and NSManagedObject class. I have a data model with an entity “UserProfile” which contains several attributes including “userId“, “username” and “clientId” which are essential. All these three attributes are marked as “String” in the Core Data Model.

The UserProfile source contains the following:

// UserProfile.h
#import <Foundation/Foundation.h>

@interface UserProfile : NSManagedObject

@property (nonatomic, retain) NSString* userId;
@property (nonatomic, retain) NSString* clientId;
@property (nonatomic, retain) NSString* userName;

// simple routine to display essential information about the user in the user table row
- (NSString*) obtainUserInfo;

// UserProfile.m
@implementation UserProfile

@dynamic userId;
@dynamic userName;
@dynamic clientId;

- (NSString*) obtainUserInfo
{
    return [NSString stringWithFormat:@"%@ - %@", [self valueForKey:@"clientId"], [self valueForKey:@"userName"]];
}

I create a new instance of the UserProfile class when the user has filled a form with some text fields and push a submit button. The relevant code chunk in the callback method is the following:

Q3AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSEntityDescription *desc = [NSEntityDescription entityForName:@"UserProfile" inManagedObjectContext:context];

// this line creates the managed object instance and stores it!
UserProfile *newProfile = [[UserProfile alloc]initWithEntity:desc insertIntoManagedObjectContext:context];

// editing properties now causes the object's state to switch to "isChanged". Changes only persist if they're explicitely saved
[newProfile setUserId: [UUIDCreater generateUUID]];
[newProfile setClientId: clientField.text];
[newProfile setUserName: userNameField.text];

NSError *error = nil;
[context save:&error];

if (!error)
{
    // now send the result data to the other controller (via the delegate object)
    [delegate newProfileCreated];

    // dismiss this view
    [self dismissModalViewControllerAnimated:YES];
}
else
{

    NSLog(@"Error: %@", error);

}

Next, a table view controller extracts all UserProfiles from the Core Data storage and displays them using prototype cells.

Okay, this all works so far, but my problem begins in this code of the UserProfilesTable class which is the ViewController of the UserProfile table view.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"userProfileEntry";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
UserProfile *obj = [_userProfileList objectAtIndex:indexPath.row];
NSLog(@"ClientId: %@", [obj valueForKey:@"clientId"]);
NSLog(@"UserName: %@", [obj valueForKey:@"userName"]);

// !!CRASH!!
NSLog(@"From Method: %@", [obj obtainUserInfo]);

cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [obj valueForKey:@"clientId"], [obj valueForKey:@"userName"]];
return cell;
}

The first two NSLogs print the correct values that I have just entered in the form to create the new user profile (and hence, the command below the crash also works as expected).
So.. with basic KVC I get the values for clientId and userName out of the UserProfile instance, but why does the app crash with an…

NSInvalidArgumentException (reason: -[NSManagedObject obtainUserInfo]: unrecognized selector sent to instance 0x6c40b90) [0x6c40b90 in this case is my UserProfile instance]

…if i try to access the method from the UserProfile class which does pretty much the same thing, just encapsulated into a separate method to improve changeability and stuff?

It seems that just because i subclassed NSManagedObject it won’t let me access my private class methods anymore. I set a breakpoint at the obtainUserInfo method and it isn’t fired which means to me that the app doesn’t even enter the method.

I found another thread which is basically the same problem as this, but the threadstarter answered it on his own and solved it by recreating the core data entity. I did the same and it didn’t solve my problem.

I’m running out of hints, so any suggestion is appreciated.

Thanks in advance.

EDIT: Fixed. Forgot to assign the class name of my UserProfile class to the Core Data Entity in the Data Model Inspector of XCode. Thanks for your help!

See Answers


source: http://stackoverflow.com/questions/11613149/objective-c-core-data-unable-to-call-private-method-on-a-nsmanagedobject-subc
Programming in objective-c: programming-in-objective-c



online applications demo