无法在控制台中显示结果

时间:2015-02-16 06:20:13

标签: ios objective-c core-data

我制作了一个将数据存储在CD中的应用程序。我想简单地将这些内容写入控制台,但我无法在控制台上打印。我做错了吗?

这是我的代码整个演示应用程序。

以下是Core_Data_Demo_xcdatamodeld

的屏幕截图

enter image description here

// AppDelegate.h

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

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

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

@end

// AppDelegate.h

-(BOOL)createNewRectangleWithHeight:(NSInteger)heightParam width:(NSInteger)widthParam{

    if (heightParam ==0 || widthParam ==0) {
        NSLog(@"The height and width must no be 0");
        return NO;
    }

    Rectangle *rect = [NSEntityDescription insertNewObjectForEntityForName:@"Rectangle" inManagedObjectContext:self.managedObjectContext];

    if (rect == nil) {
        NSLog(@"Failed to create new Rectangle");
        return NO;
    }

    rect.height = [NSNumber numberWithInt:heightParam];
    rect.width = [NSNumber numberWithInt:widthParam];

    NSError *savingError = nil;

    if ([self.managedObjectContext save:&savingError])  {
        return YES;
    } else {
        NSLog(@"Failed to save new person. Error = %@ ",savingError);
    }

    return  YES;
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self createNewRectangleWithHeight:2 width:2];

    return  YES;
}

2 个答案:

答案 0 :(得分:1)

你无法看到任何陈述,因为(我认为)事情正确运行。

如果要在控制台中检索数据并进行打印,则需要运行其他方法,例如printData或任何您想要的方法。此方法应设置NSFetchRequest并针对您的实体Rectangle执行。

- (void)printData {
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Rectangle"];

    NSError *error = nil;
    NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];
    if(error) {
        // An error occurred
    } else {
        // See the results
    }
}

用法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // [self createNewRectangleWithHeight:2 width:2];
    [self printData];
    return  YES;
}

您应该对createNew...方法发表评论,否则您会看到具有相同Rectangle和{widthheight个对象的多个条目(等于您运行该应用程序的次数) {1}}。

答案 1 :(得分:0)

您显示的代码是使用核心数据添加值,如果在添加值时没有在NSError对象中收到任何错误,则数据会成功添加到sqlite文件中。

要检查添加的值,您可以使用来自firefox的SqliteManager插件并打开sqlite文件(您可以使用NSHomeDirectory()方法获取sqlite文件位置,然后跳转到文档文件夹)。

如果您不想使用插件方式,可以随时使用NSFetchRequest来提取下面给出的数据

- (NSManagedObjectContext*)getManagedObjectContext{
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
return appDelegate.managedObjectContext;
}

- (void)fetchdataFromDatabase{
    NSManagedObjectContext *appContext = [self getManagedObjectContext];
    if(appContext!=nil){
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"YOUR_ENTITY_NAME" inManagedObjectContext:appContext];
        [fetchRequest setEntity:entity];
        NSError *error = nil;
        NSArray *fetchedObjects = [appContext executeFetchRequest:fetchRequest
                                                            error:&error];
        if(error!=nil && fetchedObjects.count!=0){
            // print your data here

        }else{
            //Print the error here
        }
    }
}