bool值不会存储在核心数据堆栈中

时间:2014-06-13 03:35:42

标签: core-data

我试图简单地在核心数据堆栈中切换BOOL属性,并使用该coredata bool来设置"状态"我的mainView中的viewDidLoad中的UISwitch。

以下场景: 开关关闭,用户点击编辑(保存)UIButton, - >将实体设置的settingSysTimeOverride属性从@NO切换到@YES 下次app启动时,在viewDidLoad中,我获取核心数据堆栈并查看settingSysTimeOverride的状态并使用它来 设置"状态"我的UISwitch。

但由于某种原因,我的值不会存储到核心数据堆栈中。 整个项目都附上了,这里也是我的代码。

Screen Shot 2014-06-12 at 22.27.28.png

MainViewController.m

#import "MainViewController.h"
#import "AppDelegate.h"

@interface MainViewController ()

@property (nonatomic, strong)NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong)NSFetchedResultsController *fetchedResultsController;

@property (strong, nonatomic) NSMutableArray *settingsArray;
@end


@implementation MainViewController

@synthesize editSetting;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization
    }
    return self;
}

- (NSManagedObjectContext *)managedObjectContext

{
    return [(AppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];

}

- (void)viewDidLoad

{
    [super viewDidLoad];

    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Setting" inManagedObjectContext:context];

    [fetchRequest setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"settingSysTimeOverride == %@", editSetting];

    [fetchRequest setPredicate:predicate];

    NSError *error;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

    if (fetchedObjects == nil) {
        NSLog(@"ERROR! %@ - %@", [error localizedDescription], [error userInfo]);
    } else {
        NSLog(@"FETCH SUCCESSFUL");
    }

    if ((fetchedObjects.count) >0) {
        // list array contents
        for (int i=0; i < (fetchedObjects.count); i++) {

            // output for debug purpose

            NSLog(@"Array-index [%i]: %@", i, fetchedObjects[i]);
        }
    }

    // check settingSysTimeOverride and set UISwitch-State
    if (editSetting.settingSysTimeOverride.boolValue == 0) {
        // turn switch to OFF - Position
        _overrideSysTimeSwitch.on = NO;
    } else {
        // turn switch to ON - Position
        _overrideSysTimeSwitch.on = YES;
    }
}

- (void)didReceiveMemoryWarning

{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)editSave:(UIBarButtonItem *)sender

{
    // toggle BOOL status of settingSysTimeOverride here
    //
}

- (IBAction)overrideSysTime:(UISwitch *)sender
{
}

- (IBAction)timeFormat:(UISwitch *)sender {
}
@end




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

#import "Setting.h"

@interface MainViewController : UIViewController <NSFetchedResultsControllerDelegate>
@property (nonatomic, strong)Setting *editSetting;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *editSaveButton

@property (strong, nonatomic) IBOutlet UILabel *overrideSysTimeLabel;
@property (strong, nonatomic) IBOutlet UILabel *timeFormatLabel;

@property (strong, nonatomic) IBOutlet UISwitch *overrideSysTimeSwitch;
@property (strong, nonatomic) IBOutlet UISwitch *timeFormatSwitch;


- (IBAction)editSave:(UIBarButtonItem *)sender;
- (IBAction)overrideSysTime:(UISwitch *)sender;
- (IBAction)timeFormat:(UISwitch *)sender;
@end



AppDelegate.h
#import <UIKit/UIKit.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

Setting.h

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

@interface Setting : NSManagedObject
@property (nonatomic, retain) NSString * settingName;
@property (nonatomic, retain) NSNumber * settingSysTimeOverride;
@property (nonatomic, retain) NSNumber * settingTimeFormat;
@end





Setting.m
#import "Setting.h"

@implementation Setting
@dynamic settingName;
@dynamic settingSysTimeOverride;
@dynamic settingTimeFormat;

@end

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这就是我做错了,我希望将来帮助其他人避免遇到同样的问题:

我忘记将NSManagedObject指向我获取的数组的开头。

NSManagedObject *matches;
matches=[objects objectAtIndex:(0)];

单件中的代码如下所示:

NSManagedObjectContext *context;
NSEntityDescription *entityDesc;
NSFetchRequest *request;

NSPredicate *pred;
NSManagedObject *matches;

NSArray *objects;

NSUInteger elementsInArray;

context = [self managedObjectContext];
entityDesc = [NSEntityDescription entityForName:@"Setting" inManagedObjectContext:context];
pred = [NSPredicate predicateWithFormat:@"(settingName = %@)", @"User-Settings"];

request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
[request setPredicate:pred];

matches = nil;
NSError *errorFS = nil;

if (!(objects = [context executeFetchRequest:request error:&errorFS])) {
    // fetch failed
    NSLog(@"Fetch failed: %@ - %@", [errorFS localizedDescription], [errorFS userInfo]);
} else {
    // fetch succeeded
    NSLog(@"Fetch Succeeded");
}

if ([objects count] == 0)
{   // does not exist -> error?
} else
{
   matches=[objects objectAtIndex:(0)];  // <- HERE WAS THE ERROR !!!!!
   // # system time override switch
   if([[matches valueForKey:@"settingSysTimeOverride"] boolValue]) {
   _sysTimeOverrideSwitch.on = YES;
} else {
   _sysTimeOverrideSwitch.on = NO;
}

}