コア データとNSManagedObject
クラスに問題があります。「 」、「」、「 」などの重要なUserProfile
属性を含むエンティティ「 」を持つデータ モデルがあります。これら 3 つの属性はすべて、コア データ モデルでは「文字列」としてマークされています。userId
username
clientId
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 に割り当てるのを忘れました。ご協力いただきありがとうございます!