0

コア データとNSManagedObjectクラスに問題があります。「 」、「」、「 」などの重要なUserProfile属性を含むエンティティ「 」を持つデータ モデルがあります。これら 3 つの属性はすべて、コア データ モデルでは「文字列」としてマークされています。userIdusernameclientId

UserProfileソースには次のものが含まれます。

// 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"]];
}

ユーザーがフォームにいくつかのテキスト フィールドを入力し、送信ボタンを押したときに、UserProfile クラスの新しいインスタンスを作成します。コールバック メソッドの関連するコード チャンクは次のとおりです。

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);

}

次に、Table View Controller が Core Data ストレージからすべての UserProfiles を抽出し、プロトタイプ セルを使用してそれらを表示します。

さて、これまでのところすべてうまくいきますが、私の問題はUserProfilesTable、UserProfile テーブル ビューの ViewController であるクラスのこのコードから始まります。

- (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;
}

最初の 2 つNSLogの s は、新しいユーザー プロファイルを作成するためにフォームに入力した正しい値を出力します (したがって、クラッシュの下のコマンドも期待どおりに機能します)。だから..基本的なKVCでは、userProfileインスタンスからclientIdとuserNameの値を取得しますが、アプリがクラッシュするのはなぜですか...

NSInvalidArgumentException(理由: -[NSManagedObject getsUserInfo]: 認識されないセレクターがインスタンス 0x6c40b90 に送信されました) [この場合の 0x6c40b90 は私の UserProfile インスタンスです]

UserProfile...ほとんど同じことを行うクラスからメソッドにアクセスしようとすると、変更可能性などを改善するために別のメソッドにカプセル化されますか?

サブクラス化NSManagedObjectしたからといって、プライベート クラス メソッドにアクセスできなくなったようです。メソッドにブレークポイントを設定しましたobtainUserInfoが、起動されません。これは、アプリがメソッドに入ることさえないことを意味します。

基本的にこれと同じ問題である別のスレッドを見つけましたが、スレッドスターターが独自に回答し、コア データ エンティティを再作成することで解決しました。私も同じことをしましたが、問題は解決しませんでした。

ヒントが不足しているので、何か提案をいただければ幸いです。

前もって感謝します。

編集:修正。XCode の Data Model Inspector で、UserProfile クラスのクラス名を Core Data Entity に割り当てるのを忘れました。ご協力いただきありがとうございます!

4

2 に答える 2

1

理由:-[NSManagedObject getUserInfo]:認識されないセレクターがインスタンス0x6c40b90に送信されました

このメッセージは、メッセージを受信して​​いる(そして処理に失敗している)オブジェクトがobtainUserInfoのインスタンスであることを意味しますNSManagedObject

[この場合の0x6c40b90は私のUserProfileインスタンスです]

いいえ、そうではありません。これはUserProfile エンティティのインスタンスですが、それでもNSManagedObject クラスのインスタンスです。valueForKey:この場合でも、アクセスされている基本プロパティは機能します。

これはおそらく、データモデルにUserProfileエンティティに指定された特定の管理対象オブジェクトサブクラスがないため、新しいインスタンスがNSManagedObjectsとして返されることを意味します。

于 2012-07-23T13:25:04.350 に答える
1

エンティティのカスタム サブクラスを に設定していないようですUserProfile。Data Model Inspector でエンティティのクラスを設定する必要があります。それ以外の場合NSManagedObjectは、ストアからフェッチするときに s が使用されます。

于 2012-07-23T13:28:54.027 に答える