核心数据日期问题

发布于 2024-08-02 07:59:49 字数 952 浏览 0 评论 0原文

我已经设置了一个核心数据模型,其中两个实体处于一对多关系(项目,对于每个项目,可以有多个 ResetDates)。我非常有信心模型设置正确。

我可以添加新项目,并在执行此操作时添加新的 ResetDate(使用当前日期,以及 [NSDate date])。我可以检索并显示项目。我遇到的问题是检索和显示 ResetDates。

更新:现在可以使用了,非常感谢下面的回答者。这是有问题的代码:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"resetDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];

NSMutableArray *sortedResets = [[NSMutableArray alloc] initWithArray:[item.resets allObjects]];
[sortedResets sortUsingDescriptors:sortDescriptors];

NSDate *oldDate = [[sortedResets lastObject] resetDate];
if ( !oldDate ) {
    oldDate = [NSDate date];
}

NSInteger numberOfDays = [self timeIntervalWithStartDate:oldDate withEndDate:currentDate];  // This function works fine, when given two NSDate objects

daysSinceLabel.text = [NSString stringWithFormat:@"%d days", numberOfDays];

I've got a Core Data model set up, with two entities in a one-to-many relationship (Items, and for each item, there can be multiple ResetDates). I'm pretty confident the model is set up correctly.

I can add new Items, and when doing so, add a new ResetDate (using the current date, with [NSDate date]). I can retrieve and display Items. What I'm having trouble with is retrieving and displaying the ResetDates.

Updated: It works now, thanks very much to the answerers below. Here's the code in question:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"resetDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];

NSMutableArray *sortedResets = [[NSMutableArray alloc] initWithArray:[item.resets allObjects]];
[sortedResets sortUsingDescriptors:sortDescriptors];

NSDate *oldDate = [[sortedResets lastObject] resetDate];
if ( !oldDate ) {
    oldDate = [NSDate date];
}

NSInteger numberOfDays = [self timeIntervalWithStartDate:oldDate withEndDate:currentDate];  // This function works fine, when given two NSDate objects

daysSinceLabel.text = [NSString stringWithFormat:@"%d days", numberOfDays];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-08-09 07:59:49

首先,NSArray -objectAtIndex: 不会返回 nil,如果您向其传递超出范围的索引,它将引发 NSRangeException,当你不确定索引,需要使用-objectAtIndex:时,你必须先调用-count方法来检查。

更重要的是,NSArray 不能包含 nil 值,因为 nil 不是一个对象。

然后,不,它不是一个 NSDate 对象,当您询问 item 它的重置关系 (item.resets) 时,您会得到一个 NSSet 包含 Reset 返回托管对象,而不是 NSDate 对象,您想要的是返回的 resetDate 属性code>Reset 托管对象,也许是这样的:

// NSArray -lastObject method return nil if the array is empty
// Sending messages to nil is Ok there, so we can call resetDate directly

NSDate *oldDate = [[sortedResets lastObject] resetDate];
if ( !oldDate ) {
    oldDate = [NSDate date];
}

希望有所帮助,并且我的英语是可以理解的......

First, NSArray -objectAtIndex: is not returning nil if you pass it an index that is out of the bounds, it will raise an NSRangeException, when you're not sure about the index, and need to use -objectAtIndex:, you have to call the -count method before to check.

More importantly, an NSArray can't contain a nil value, as nil is not an object.

Then, no, it's not an NSDate object, when you ask item for it's resets relationship (item.resets), you get an NSSet that contain Reset managed objects in return, not NSDate objects, what you want is the resetDate attribute of the returned Reset managed objects, maybe something like this :

// NSArray -lastObject method return nil if the array is empty
// Sending messages to nil is Ok there, so we can call resetDate directly

NSDate *oldDate = [[sortedResets lastObject] resetDate];
if ( !oldDate ) {
    oldDate = [NSDate date];
}

Hope that help, and that my English is understandable...

固执像三岁 2024-08-09 07:59:49

也许用 : 替换 :

NSDate *oldDate = sortedResets[0];

NSDate *oldDate = [sortedResets objectAtIndex:0];

有所帮助。 sortedResets 是一个 NSArray 对象,而不是一个 C 数组;)

Maybe replacing :

NSDate *oldDate = sortedResets[0];

with :

NSDate *oldDate = [sortedResets objectAtIndex:0];

will help. sortedResets is an NSArray object, not a C array ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文