0

運が悪かったので、似たようなものを探し回ったので、問題を説明してコードを貼り付けようと思います。Core Data を使用するアプリケーションがあり、(多くの関係への) 例外を除いて、それぞれの textFields からデータを保存および取得できます。これらは保存され、取得時にセットとして返されると思います。私は NSSet を読み、いくつかのコードを見ましたが、それでもコーディング方法がわかりません。

助けてくれてありがとう。ハドソン

- (IBAction) findContact
{
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Place" 
                                              inManagedObjectContext:context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
[fetchRequest setEntity:entity];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"placeName = %@", placeName.text];
[fetchRequest setPredicate:pred];
Place *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];
if ([objects count] == 0) {
    status.text = @"No matches";
}else{

    matches = [objects objectAtIndex:0];
    address.text = [matches valueForKey:@"address"];
    city.text = [matches valueForKey:@"city"];
    state.text = [matches valueForKey:@"state"];
    zip.text = [matches valueForKey:@"zip"];
    phone1.text = [matches valueForKey:@"phone1"];
    email.text = [matches valueForKey:@"email"];
    website.text = [matches valueForKey:@"website"];
    phone2.text = [matches valueForKey:@"phone2"];
    about.text = [matches valueForKey:@"about"];
    photoName.text = [[matches photo]valueForKey:@"photoName"];
    status.text = [NSString  stringWithFormat:@"%d matches found", [objects count]];
    NSSet *groupForSections = [groupForSections valueForKey:@"sections"];
    for (Group *group in groupForSections) {
        NSLog(@"group name = %@", [groupForSections valueForKey:@"groupName"]);
        groupName.text = [group valueForKey:@"groupName"];
    NSSet *sectionForPlaces = [sectionForPlaces valueForKey:@"places"];
    for (Section *section in sectionForPlaces) {
        sectionName.text = [section valueForKey:@"sectionName"];
        NSLog(@"section name = %@", [section valueForKey:@"sectionName"]);
        }
    }
    }
    }

![ここに画像の説明を入力][1]

4

1 に答える 1

0

フォローアップのコメントによると、Place は実際には Section と対多の関係を持っておらず、Section は実際には Group と対多の関係を持っていません。代わりに、グループにはセクションとの対多の関係 (セクション) があり、セクションの逆の対 1 の関係はグループと呼ばれます。また、Section は Place と対多の関係 (Place) を持ち、Place の逆の対 1 の関係を Section と呼びます。

これらの仮定がすべて正しい場合 (モデルを見ないと、それが正しいかどうかを判断するのは困難です)、コードは次のようになります。

NSManagedObject *sectionForPlace = [matches valueForKey:@"section"]; 
sectionName.text = [sectionForPlace valueForKey:@"sectionName"];

NSManagedObject *groupForSection = [sectionForPlace valueForKey:@"group"];
gromName.text = [groupForSection valueForKey:@"groupName"];

このコードは、作成した NSManagedObject クラスを介してフィールドに直接アクセスするわけではありません。そのようにしたい場合は、これも機能するはずです。

于 2011-11-17T16:41:30.413 に答える