0

文字列 "login" と "pass" を持つエンティティ LoginPass があります。登録を作成しましたが、UItextField のテキストが Core Data の「ログイン」の 1 つと等しいかどうかを比較する必要があります。そのようなログインが作成された場合、パスワード UITextField のテキストを比較する必要があり、それが等しい場合、アプリはメイン ビューを開きます。述語に問題があり、LoginPass * newEntity を使用して fetchResult を使用して新しいエンティティを初期化する方法がわかりません

更新: わかりました、私が欲しいものを理解しようとします。私の英語が下手で、テキストが正しくありません。エンティティ「LoginPass」を含むコア データがあり、「ログイン」(ニックネームを保存)、「パスワード」(ログイン用のパスワードを保存)、およびいくつかのデータで構成されています。UITextField「ログイン」のニックネームがコアデータに含まれているかどうかを確認する必要があります。そのようなログインがない場合はアラートが表示され、そのようなログインが Core Data にある場合はパスワードを確認し、自動回復が成功するとメイン ビューが表示されます。

//from standart singleton Appdelegate i delegated manageObjectContext
AppDelegate * appDelegate = (AppDelegate *) [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = appDelegate.managedObjectContext;
//assigning. Can I use myEntity in my further mode of action to change datas in entity
NSEntityDescription * myEntity = [NSEntityDescription entityForName:@"LoginPass" inManagedObjectContext:context];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc]init];
//I need search in Core Data with nickname that's in UITextField "login"
NSPredicate *predicateMe = [NSPredicate predicateWithFormat:@"login==%@",login.text];
//login.text - text from UITextField, myEntity.login - string value in key "login" in COre Data
//Or i must write:
NSPredicate *predicateMe = [NSPredicate predicateWithFormat:@"%@ LIKE %@",myEntity.login,login.text];
fetchRequest.predicate = predicateMe;
[fetchRequest setEntity:myEntity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor * sortirovka = [[NSSortDescriptor alloc] initWithKey:@"login" ascending:YES];
NSArray * sortArray = [NSArray arrayWithObjects:sortirovka,nil];
[fetchRequest setSortDescriptors:sortArray];
NSFetchedResultsController * fetchResult = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:@"Login"];
//thanks for this part of code, i understand that is must be
 NSError *error = nil;
if (![fetchResults performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
//can i use myEntity for changing datas in my "LoginPass" or i need create:
LoginPass *newEntity = ?;

しかし、フィールド「ログイン」に入力されたニックネームを持つユーザーのデータを変更するための LoginPass *newEntity を作成する方法がわかりません

4

1 に答える 1

0

次に、フェッチを実行します。

NSError* error;
if(![fetchResult performFetch  : &error])
{
    NSLog(@"Failed with error: %@",error);
    return;  // do whatever to exit from the method (return the proper value
             // if it doesn't return void).
}
NSArray* fetchedObjects=[fetchResult fetchedObjects];
// Do this assertion if you expect that this will fetch no less
// and no more than one object:
NSAssert(fetchedObjects.count==1, @"Zero or too many objects"); 
NSManagedObject* newEntity= fetchedObjects[0];

私はそれをテストしておらず、あなたのコードを完全には理解していないため、よくわかりませんが、うまくいくはずです。

詳細はこちら: NSFetchedResultsController ドキュメント.

于 2012-12-13T22:03:51.693 に答える