Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 关于动态更改默认核心数据模型内容的建议_Objective C_Xcode_Core Data_Nsmanagedobject - Fatal编程技术网

Objective c 关于动态更改默认核心数据模型内容的建议

Objective c 关于动态更改默认核心数据模型内容的建议,objective-c,xcode,core-data,nsmanagedobject,Objective C,Xcode,Core Data,Nsmanagedobject,为了更好地说明问题,我添加了下图: 嗨 我正在寻找一个最佳的起点来直接改变存储在我的核心数据模型中的数据——作为这个领域的新手来说。从我的阅读中,我非常自信我不应该碰我的NSArrayController,这是我的本能,我应该始终处理模型。这是有道理的,但因为我使用了绑定和核心数据,xcode为我生成了所有内容,我自己也没有从头构建类的感觉 对于我的初始任务,我有一个“作业”实体和NSArrayController。它有一个jobTotalHours属性,它是一个00:00:00格式的字符串,

为了更好地说明问题,我添加了下图:

我正在寻找一个最佳的起点来直接改变存储在我的核心数据模型中的数据——作为这个领域的新手来说。从我的阅读中,我非常自信我不应该碰我的
NSArrayController
,这是我的本能,我应该始终处理模型。这是有道理的,但因为我使用了绑定和核心数据,xcode为我生成了所有内容,我自己也没有从头构建类的感觉

对于我的初始任务,我有一个“作业”实体和
NSArrayController
。它有一个jobTotalHours属性,它是一个00:00:00格式的字符串,并且在
NSTableView
中为每个作业都有一个相应的“小时”列。除此之外,我还有一个秒表按钮,它链接到旁边的文本字段,以00:00:00字符串的形式显示时间。我有一个工作类,它启动和停止计时器计数,并以小时、分钟和秒为增量显示

我需要做的是使计时器将时间添加到当前作业的jobTotalHours属性中,该作业在
NSTableView
中突出显示。单独的文本字段现在已绑定为显示当前突出显示的小时数列的时间,以便处理该部分。换句话说,计时器最初是将时间添加到测试变量中,并出于测试原因将其显示在自主文本字段中。现在,我需要它为表视图中突出显示的任何作业添加时间,并且我需要以编程方式访问模型,而不必确定首先要采取什么步骤

提前谢谢你的建议。如果有任何用处,我会在下面加入timer类。我很确定这很粗糙,很糟糕,但它很有效:

timerController.h:

#import <Cocoa/Cocoa.h>
BOOL timerStarted;
int timerCount;
int timerSeconds;
int timerMinutes;
int timerHours;
NSString *timerString;
NSString *timerFieldSeconds;
NSString *timerFieldMinutes;
NSString *timerFieldHours;

@interface timerController : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    NSTimer *timerNoOne;
    IBOutlet NSCell *timerOneOutputLabel;
    IBOutlet id timerClockField;
}
-(IBAction)toggleTimerClock:(id)sender;
@property (assign) IBOutlet NSWindow *window;

@end
#import "timerController.h"


@implementation timerController
-(IBAction)toggleTimerClock:(id)sender
{
    if (timerStarted==FALSE) {
        timerStarted = TRUE;
    } else {
        timerStarted = FALSE;
    }
}

@synthesize window;


- (void) awakeFromNib {
    // clear timer 
    [timerClockField setStringValue:@"00:00:00"];
    // initialize timer to count each second
    timerNoOne = [NSTimer scheduledTimerWithTimeInterval:1 
                            target:self 
                            selector:@selector(updateTimerNoOne:) 
                            userInfo:nil 
                            repeats:YES];   

}


- (void) updateTimerNoOne:(NSTimer *) timer {
    if (timerStarted==FALSE) {
        // do nothing. Timer is switched off.
    } else {
        timerCount = timerCount + 1;
        timerSeconds = fmod(timerCount, 60);
        timerMinutes = floor(timerCount / 60);
        timerHours = floor(timerCount / 3600);

        if (timerSeconds < 10) { // add a leading 0 for formatting reasons.
            timerFieldSeconds = [NSString stringWithFormat:@"0%d",timerSeconds];
        } else {
            timerFieldSeconds = [NSString stringWithFormat:@"%d",timerSeconds];
        }

        if (timerMinutes < 10) {
            timerFieldMinutes = [NSString stringWithFormat:@"0%d",timerMinutes];
        } else {
            timerFieldMinutes = [NSString stringWithFormat:@"%d",timerMinutes];
        }

        if (timerHours < 10) {
            timerFieldHours = [NSString stringWithFormat:@"0%d",timerHours];
        } else {
            timerFieldHours = [NSString stringWithFormat:@"%d",timerHours];
        }

        NSString *timerString = [NSString stringWithFormat:@"%@:%@:%@",timerFieldHours,timerFieldMinutes,timerFieldSeconds];
        //[timerClockField setStringValue:timerString];

    }
}
@end
它很好地完成了上述任务。我现在想要一个指向
NSManagedObject
的出口,这样我就可以直接操作其中的资产,同时保持指向
NSTableView
的出口,以检测行选择中的更改。我读到子类应该是

@interface modelUtilController : NSManagedObject
我将其更改为,并包括数据模型的一个出口。这破坏了对行选择更改的原始检测,所以我现在做错了什么。也许我必须把这个子类分成2个

更新:可能已完成


好的,我想我已经解决了这个问题,三天了。据我所知,它正在工作,但我还没有完全投入工作。基本上,我创建了一个单独的函数,每秒从计时器调用一次:

void amendTotalHours(id anObject)
void amendTotalHours(id anObject)
此函数使用我的作业
NSArrayController
,然后使用以下命令在小时列中查找当前值:

NSArray *selectedObjectsArray = [anObject selectedObjects];
NSManagedObjectModel *firstSelectedObject = [selectedObjectsArray objectAtIndex:0];
NSString *readCurrentTime = [firstSelectedObject valueForKey:@"jobTotalHours"];
NSArray *selectedObjectsArray = [anObject selectedObjects];
NSManagedObjectModel *firstSelectedObject = [selectedObjectsArray objectAtIndex:0];
NSString *readCurrentTime = [firstSelectedObject valueForKey:@"jobTotalHours"];
然后,我将格式化为00:00:00的时间字符串转换为总秒数的整数。我为来自计时器的每个调用添加一个,然后将秒数转换回00:00:00格式的字符串。最后,我使用以下命令将其发送回NSArrayController:

[firstSelectedObject setValue:[NSString stringWithFormat:@"%@", timeValue] forKey:@"jobTotalHours"];
[firstSelectedObject setValue:[NSString stringWithFormat:@"%@", timeValue] forKey:@"jobTotalHours"];

然后松了一口气(也许是暂时的)。

好的,我想我已经解决了这个问题,三天了。据我所知,它正在工作,但我还没有完全投入工作。基本上,我创建了一个单独的函数,每秒从计时器调用一次:

void amendTotalHours(id anObject)
void amendTotalHours(id anObject)
此函数使用我的作业
NSArrayController
,然后使用以下命令在小时列中查找当前值:

NSArray *selectedObjectsArray = [anObject selectedObjects];
NSManagedObjectModel *firstSelectedObject = [selectedObjectsArray objectAtIndex:0];
NSString *readCurrentTime = [firstSelectedObject valueForKey:@"jobTotalHours"];
NSArray *selectedObjectsArray = [anObject selectedObjects];
NSManagedObjectModel *firstSelectedObject = [selectedObjectsArray objectAtIndex:0];
NSString *readCurrentTime = [firstSelectedObject valueForKey:@"jobTotalHours"];
然后,我将格式化为00:00:00的时间字符串转换为总秒数的整数。我为来自计时器的每个调用添加一个,然后将秒数转换回00:00:00格式的字符串。最后,我使用以下命令将其发送回NSArrayController:

[firstSelectedObject setValue:[NSString stringWithFormat:@"%@", timeValue] forKey:@"jobTotalHours"];
[firstSelectedObject setValue:[NSString stringWithFormat:@"%@", timeValue] forKey:@"jobTotalHours"];
然后(也许是暂时的)松一口气