核心数据日期问题
我已经设置了一个核心数据模型,其中两个实体处于一对多关系(项目,对于每个项目,可以有多个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,
NSArray -objectAtIndex:
不会返回nil
,如果您向其传递超出范围的索引,它将引发NSRangeException
,当你不确定索引,需要使用-objectAtIndex:
时,你必须先调用-count
方法来检查。更重要的是,
NSArray
不能包含nil
值,因为nil
不是一个对象。然后,不,它不是一个
NSDate
对象,当您询问item
它的重置关系 (item.resets
) 时,您会得到一个NSSet
包含Reset
返回托管对象,而不是NSDate
对象,您想要的是返回的resetDate
属性code>Reset 托管对象,也许是这样的:希望有所帮助,并且我的英语是可以理解的......
First,
NSArray -objectAtIndex:
is not returningnil
if you pass it an index that is out of the bounds, it will raise anNSRangeException
, 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 anil
value, asnil
is not an object.Then, no, it's not an
NSDate
object, when you askitem
for it's resets relationship (item.resets
), you get anNSSet
that containReset
managed objects in return, notNSDate
objects, what you want is theresetDate
attribute of the returnedReset
managed objects, maybe something like this :Hope that help, and that my English is understandable...
也许用 : 替换 :
会
有所帮助。 sortedResets 是一个 NSArray 对象,而不是一个 C 数组;)
Maybe replacing :
with :
will help. sortedResets is an NSArray object, not a C array ;)