0

コアデータの概念を初めて使用して、情報を簡単に挿入および取得します..

私はuserInfoAppDelegate.huserInfoAppDelegate.mDisplayController.h、を持っていDisplayController.mます。

これで、デリゲート ファイルにデータを正常に追加および取得できました。コードは次のとおりです。

userInfoAppDelegate.h

#import <UIKit/UIKit.h>
#import "HomeController.h"

@class RootViewController;

@interface UserInfoAppDelegate : UIResponder <UIApplicationDelegate>{    
    HomeController *hController;
    UIWindow *Window;        
}    
@property (nonatomic, strong) UINavigationController *navigationController;
@property (nonatomic, strong) RootViewController *rootViewController;        
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;    
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;    
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end

userInfoAppDelegate.m

@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{         
   //some code...

UserInfo *newUser = [NSEntityDescription insertNewObjectForEntityForName:@"UserInfo"
             inManagedObjectContext:self.managedObjectContext];    

if (newUser != nil){             
    newUser.firstName = @"test"; 
    newUser.lastName = @"test";
    newUser.userName=@"b@test.com";
    // newUser.bDate = [NSDate date] ;
    newUser.department=@"Admin";

    NSError *savingError = nil;        
    if ([self.managedObjectContext save:&savingError])
    { 
        NSLog(@"Successfully saved the context."); 
    } 
    else
    { 
        NSLog(@"Failed to save the context. Error = %@", savingError);
    } 
}     

else {        
    NSLog(@"Failed to create the new person.");
}   

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];    

/* Here is the entity whose contents we want to read */ 

NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserInfo"
       inManagedObjectContext:self.managedObjectContext];    

/* Tell the request that we want to read the contents of the Person entity */
[fetchRequest setEntity:entity]; 
NSError *requestError = nil;    

/* And execute the fetch request on the context */ 
NSArray *users = [self.managedObjectContext executeFetchRequest:fetchRequest
                    error:&requestError];

NSLog(@"%@",users);       
if ([users count] > 0){
    NSUInteger counter = 1; 

    for (UserInfo *thisUser in users){            
        NSLog(@"Person %lu First Name = %@", (unsigned long)counter, thisUser.firstName);
        NSLog(@"Person %lu Last Name = %@", (unsigned long)counter,
              thisUser.lastName);
        NSLog(@"Person %lu Department = %@", (unsigned long)counter,
              thisUser.department ); counter++;
    }
}

else { 
    NSLog(@"Could not find any Person entities in the context.");
}

  //some code...

}

// 上記のコードは適切な結果でうまく機能しましたが、同じコードがDisplayController.hANDでは機能しませんDisplayController.mでした。

4

1 に答える 1

0

コントローラーで AppDelegate 参照を取得してから、使用している場所self.managedObjectContextで使用しますdelegate.managedObjectContext

ここに小さなサンプルがあります

userInfoAppDelegate *delegate = (userInfoAppDelegate *)[[UIApplication sharedApplication] delegate];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserInfo"
       inManagedObjectContext:delegate.managedObjectContext]; 
于 2012-11-30T06:39:07.740 に答える