0

iOS アプリケーションで Core Data を使用しています。私はこのようなモデルを持っています: 私の質問は、どのように自分のデータにアクセスできるかです。たとえば、「id_page」で本を取得する方法は?? 答えてくれてありがとう

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

    @class Note, Page;

    @interface Book : NSManagedObject

    @property (nonatomic, retain) NSString * id_book;
    @property (nonatomic, retain) NSSet *page;
    @property (nonatomic, retain) NSSet *note;

    -(Note*)noteForPage;
    -(Page*)pageForPageNumber:(int)pageNumber;

    @end

    @interface Book (CoreDataGeneratedAccessors)

    - (void)addPageObject:(Page *)value;
    - (void)removePageObject:(Page *)value;
    - (void)addPage:(NSSet *)values;
    - (void)removePage:(NSSet *)values;
    - (void)addNoteObject:(Note *)value;
    - (void)removeNoteObject:(Note *)value;
    - (void)addNote:(NSSet *)values;
    - (void)removeNote:(NSSet *)values;
    @end

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


@interface Page : NSManagedObject

@property (nonatomic, retain) NSString * id_page;

@end

私はこのような本とページを作成しました:

B

ook *book = [NSEntityDescription
                  insertNewObjectForEntityForName:@"Book"
                  inManagedObjectContext:context];

    Page *page1 = [NSEntityDescription
                                       insertNewObjectForEntityForName:@"Page"
                                       inManagedObjectContext:context];
    [page1 setValue:@"firstPage" forKey:@"id_page"];

    Page *page2 = [NSEntityDescription
                   insertNewObjectForEntityForName:@"Page"
                   inManagedObjectContext:context];
    [page2 setValue:@"secondPage" forKey:@"id_page"];

    Page *page3 = [NSEntityDescription
                   insertNewObjectForEntityForName:@"Page"
                   inManagedObjectContext:context];
    [page3 setValue:@"ThirdPage" forKey:@"id_page"];

    // create a set of pages
    NSSet *pageSets = [[NSSet alloc]initWithObjects:page1,page2,page3, nil];
    [book addPage:pageSets];
4

2 に答える 2

0

Use NSFetchRequest here は、コア データを使い始めるための優れたチュートリアルです: http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started

于 2012-04-26T14:11:30.840 に答える
0
- (Book *)bookWithIdStr:(NSString *)idStr {
  NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Book"];
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id_book MATCHES %@", idStr];
  fetchRequest.predicate = predicate;
  return [[self.managedObjectContext executeFetchRequest:fetchRequest error:nil] lastObject];
}
于 2012-04-26T14:13:35.113 に答える